I would like to use itertools.product
or a similar command in order to obtain the cartesian product of n
copies of an iterator iter
with itself, where n
is a variable. I tried constructing it recursively by
import itertools
prod_iter = iter
for i in xrange(n-1):
prod_iter = itertools.product(prod_iter,iter)
But starting with, say, iter=xrange(2)
, n=3
and running
for i in prod_iter:
print i
I get as an output
((0,0),0)
((0,0),1)
((0,1),0)
...
and not
(0,0,0)
(0,0,1)
(0,1,0)
...
as I would like. Is there a way to do this?