Here is a solution using recursion, combs_r
has accum
digest head
(the next list in line) to produce a fatter accum0
, then and calls itself ("recursion") with tail
(the remaining lists) and the now fatter accum accum0
.
Might be heavy user of memory as each call to combs_r
adds a new namespace, until the end when it all unwinds. Person more knowlegable in Python internals might comment on this.
Pays to learn prolog, IMHO.
def combs(ll):
if len(ll) == 0:
return []
if len(ll) == 1:
return [[item] for item in ll[0]]
elif len(ll) == 2:
return lmul(ll[0], [[item] for item in ll[1]])
else:
return combs_r(ll[1:], ll[0])
def combs_r(ll, accum):
head = ll[0]
tail = ll[1:]
accum0 = []
accum0 = lmul(head, accum)
if len(tail) == 0:
return accum0
else:
return combs_r(tail, accum0)
def lmul(head, accum):
accum0 = []
for ah in head:
for cc in accum:
#cc will be reused for each ah, so make a clone to mutate
cc0 = [x for x in cc]
cc0.append(ah)
accum0.append(cc0)
return accum0
sampleip = [['a','b','c'],[1,2], ['A', 'B']]
sampleip2 = [['a','b','c'],[1,2]]
sampleip1 = [['a','b','c']]
sampleip0 = []
print combs(sampleip0)
print combs(sampleip1)
print combs(sampleip2)
print combs(sampleip)