
May 23, 2008
8:33 a.m.
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