[Python-Dev] A `cogen' module [was: Re: PEP 218 (sets); moving set.py to Lib]
Guido van Rossum
guido@python.org
Wed, 28 Aug 2002 11:02:25 -0400
> Even if all the arguments are re-iterable containers the recursive call
> produces a lazy generator object - the cartesian product of the tail. I
> don't want to read it eagerly into a list.
And I wasn't proposing that.
def cartesian(*sequences):
if len(sequences) == 0:
yield []
else:
head, tail = sequences[:-1], sequences[-1]
tail = list(tail) # <--- This is what I was proposing
for x in cartesian(*head):
for y in tail:
yield x + [y]
--Guido van Rossum (home page: http://www.python.org/~guido/)