This prints nothing:
from itertools import combinations
comb = combinations([1,2], 3)
for i in comb:
print(i)
I want output as :
(1,2,2) (1,2,1) (1,1,2) (1,1,1) (2,1,2) (2,1,1) (2,2,1) (2,2,2)
This prints nothing:
from itertools import combinations
comb = combinations([1,2], 3)
for i in comb:
print(i)
I want output as :
(1,2,2) (1,2,1) (1,1,2) (1,1,1) (2,1,2) (2,1,1) (2,2,1) (2,2,2)
Seems like you just want the product
, not combinations
:
from itertools import product
for i in product([1, 2], repeat=3):
print(i)
combinations
is getting you unique combinations without reusing elements within any combination, which means pulling three elements from two source elements is impossible. It's also order-insensitive, so it wouldn't give you (1, 2)
and (2, 1)
even if you only asked for combinations of size 2
(only permutations
would do that). In your case, you seem to want to cycle every element through every index, allowing repeats (which combinations
/permutations
won't do) and order-sensitive (which combinations_with_replacement
won't do), which leaves product
.
You can't generate 3 element combinations from a 2 item list. Try this:
comb = combinations([1,2]*3, 3)
This basically extends the iterable to a 6-item list ([1, 2, 1, 2, 1, 2]
).