[Python-ideas] Function to unnest for-statements
Leif Walsh
adlaiff6 at gmail.com
Fri May 23 10:33:05 CEST 2008
On Fri, 23 May 2008, Leif Walsh wrote:
> I've worked this out (I'm sure I did this in intro CS once before...),
> although there is probably some optimization trick I'm missing, and
> I'm probably doing some silly double-allocation thing or something.
>
> >>> def combine(lists):
> ... if len(lists) is 0:
> ... return [[]]
> ... first = combine(lists[0:-1])
> ... total = []
> ... for item in lists[-1]:
> ... total += [[item] + subset for subset in first]
> ... return total
Oops, that should probably be:
... for subset in first:
... total += [subset + [item] for item in lists[-1]]
to maintain order. My excuse is that it's past my bedtime.
--
Cheers,
Leif
More information about the Python-ideas
mailing list