I want to be able to generate a conditional product. So similar to this answer: All combinations of a list of lists
I wanted to use itertools.product(*listOfLists)
. However, my problem is that the inclusion of one element from one list means other lists must be consulted for the product.
Example:
colors = ['red', 'blue', 'green']
fruits = ['apple', 'orange', 'banana']
locations = ['indoors', 'outdoors']
indoor_choices = ['bathroom', 'bedroom', 'kitchen']
green_choices = ['forest', 'light', 'dark']
Here, we want to consider always every possible choice of color, fuit, and location. However, in the case of 'indoor', we also want to consider indoor_choices, and in the case of 'green' being in a possible choice, we also want to choose a more specific color of green. It's kind of a tree of possibilities where some branches keep branching and others do not.
So in this silly example above you could do a for loop like so:
for c in colors:
for f in fruits:
for l in locations:
# etc
but then we encounter the problem of what happens when two different categories have possible branching based on this choice.
A simple (hacky) solution would be just to manually code conditions and put for loops inside of them:
for c in colors:
for f in fruits:
for l in locations:
if c == 'green' and l == 'indoor':
for gc in green_choices:
for ic in indoor_choices:
# output
elif c == 'green':
for gc in green_choices:
# output
elif l == 'indoor':
for gc in green_choices:
# output
else:
# output
but imagine the horror when there are N lists where M of them have additional branching. Or even worse, there is nested additional branching ... basically this hack doesn't scale.
Any ideas? This problem has proved itself to be deceptively hard!