Issue
I have a list which contain values input = [1, 2, 3] and would need all the possible combination of this values i.e. Output = [[], [1], [2], [3], [1,2], [1,3], [2, 3], [1,2,3]].
I have used below code but it is not showing exact output values.
output = permutations([1, 2, 3], 2)
for i in output:
print(list(i))
Can someone help me with this?
Solution
From the output you're giving, it seems you want the combinations and not the permutations.
You can iterate over all possible valid lengths (0 to 3) and create a sequence like that.
import itertools as it
list(it.chain.from_iterable(it.combinations([1, 2, 3], i) for i in range(4)))
will output:
[(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]
Answered By - Nathan Furnal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.