python input for itertools.product

Posted by user364249 on Stack Overflow See other posts from Stack Overflow or by user364249
Published on 2010-06-11T06:41:29Z Indexed on 2010/06/11 6:53 UTC
Read the original article Hit count: 180

Filed under:

Looking for a way to simulate nested loops (or a cartesian product) i came across the itertools.product function. i need a function or piece of code that receive a list of integers as input and returns a specific generator.

example:
input = [3,2,4] -> gen = product(xrange(3),xrange(2),xrange(4))
or
input = [2,4,5,6] -> gen = product(xrange(2),xrange(4),xrange(5),xrange(6))

as the size of the lists varies i am very confused in how to do that without the need of a lot of precoding based on a crazy amount of ifs and the size of the list.

also is there a difference in calling product(range(3)) or product(xrange(3))?

© Stack Overflow or respective owner

Related posts about python