The following code works, but appears verbose.
def gen(l):
for x in range(l[0]):
for y in range(l[1]):
for z in range(l[2]):
yield [x, y, z]
l = [1, 2, 3]
print(list(gen(l)))
>>>[[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 1, 0], [0, 1, 1], [0, 1, 2]]
My intention was to cut down on LOC with itertools.product. Here's what I came up with.
from itertools import product
def gen(l):
for x, y, z in product(map(range, l)):
yield [x, y, z]
l = [1, 2, 3]
print(list(gen(l)))
ValueError: not enough values to unpack (expected 3, got 1)
Is there a different way to use itertools.product so that there are enough values to unpack?
You need to pass the elements of the map
iterator to product
separately with *
:
for x, y, z in product(*map(range, l))
Incidentally, with another map
call, you could save another line, skip the overhead of a Python generator, and do all the work in C:
def gen(l):
return map(list, product(*map(range, l)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With