(newbie) N-uples from list of lists

bonono at gmail.com bonono at gmail.com
Fri Dec 2 09:51:03 EST 2005


Martin Miller wrote:
> I'd be interested in seeing the one liner using reduce you mentioned --
> how it might be done that way isn't obvious to me.
>
> Another aspect of Taschuk's solution I like and think is important is
> the fact that it is truly iterative in the sense that calling it
> returns a generator which will yield each of the combinations, one at
> time.  All the others create and return all the combinations at once
> (as I suspect the one liner using reduce you mention does, too).
>
> As you point out, "best" is always in the eyes of the beholder.
>
> "Best" regards, ;-)

def combine_lol(seq): return reduce(lambda x,y: (a+(b,) for a in x for
b in y), seq, [()])

I use generator expression but being a reduce, it will still goes all
the way till the last sequence before you can have any output. That is
the nature of the problem anyway. You can use scanl equivalent to have
intermediate result if the problem allows but I don't that is true in
this case.

The python community is quite against this kind of thing and treat
map/filter/reduce as plague, it is described as ugly/messy :-)




More information about the Python-list mailing list