Is there a recursive solution for the following problem: For a given list of lists all combinations of the members of these lists should be calculated, for example:
[list('123'), list('ABCD'), list('ab')]
will produce:
['a', 'A', '1']
['a', 'A', '2']
['a', 'A', '3']
['a', 'B', '1']
['a', 'B', '2']
['a', 'B', '3']
['a', 'C', '1']
['a', 'C', '2']
['a', 'C', '3']
['a', 'D', '1']
['a', 'D', '2']
['a', 'D', '3']
['b', 'A', '1']
['b', 'A', '2']
['b', 'A', '3']
['b', 'B', '1']
['b', 'B', '2']
['b', 'B', '3']
['b', 'C', '1']
['b', 'C', '2']
['b', 'C', '3']
['b', 'D', '1']
['b', 'D', '2']
['b', 'D', '3']
The following iterative program generates the above output:
rec = [list('123'), list('ABCD'), list('ab')]
done = False
vect = [0 for i in rec]
while not done:
pass
for j in range(len(rec[0])):
vect[0] = j
print [rec[i][vect[i]] for i in range(len(rec))][::-1]
c = not False
for k in range(1, len(rec)):
if c:
c = (vect[k]+c) >= len(rec[k])
if c:
vect[k] = 0
else:
vect[k] += 1
if c:
done = True
but I am looking for a recursive solution which probably will be shorter and more readable ?