I have a dictionary containing lists of various lengths, and I would like the general algorithm to produce combinations of all the elements in the lists. An example code example is shown below:
bob = {'a':['a','b','c'],
'b':[0],
'c':['x','y']}
for i in bob['a']:
for j in bob['b']:
for k in bob['c']:
print("%s - %s - %s"%(str(i),str(j),str(k)))
This is the desired result for a particular bob dictionary, and it outputs the result:
a - 0 - x
a - 0 - y
b - 0 - x
b - 0 - y
c - 0 - x
c - 0 - y
However, I need a general algorithm. Can I use recursion to generalize this so that it can handle dictionaries with an arbitrary number of keys, if so, how?
Additional information: the values corresponding to the keys will always be 1D lists
source
share