I want to put several data files through two modules to process them, using every combination of several settings each on several parameters for each module. The obvious way to do this is with a nested for loop, but by the time you get to 7+ nested for loops, no. I want to make this more elegant than that.
I've already read several very similar questions, but while this one reveals that I probably want to use itertools, it only iterates through number sequences, while I want to iterate through lists of strings that are contained as values within dictionaries; this other one reveals that what I want is called a Cartesian product, but not how to make that out of dictionary values; and while this one combines dictionaries of lists in a Cartesian product, I want the output to be a list of lists as in the previous linked question, not a list of dictionaries.
In:
video = ["It's Friday.mp4",'Hot Koolaid.mov','The Water Buffalo Song.mp4']
CC = {'size':['6','10','14'],'font':['Courier New'],'color':['black','white'],'language':['English']}
Noise = {'CRT':['speckles','rising stripes','no signal'],'sound':['white','crackle']}
Out:
[['It's Friday.mp4','6','Courier New','black','English','speckles','white'],
['Hot Koolaid.mov','6','Courier New','black','English','speckles','white']
...
['The Water Buffalo Song.mp4','14','Courier New','white','English','no signal','crackle']]
I'm pretty sure I want to use itertools
, and that what I want to make is a Cartesian product of lists. I think the hardest thing at the moment is to draw those lists out of the dictionaries and put the combinations of their elements into lists.
_________Edited:____________
In the process of checking out the answer I then accepted, I found that it's important (for my purposes here) for all parameters to be in lists, even if there's only one value being considered; a string without square brackets will be iterated over one character at a time.
The ugly nested for loop looks like:
for vid in video:
for siz in CC['size']:
for fon in CC['font']:
for col in CC['color']:
for lan in CC['language']:
for crt in Noise['CRT']:
for sou in Noise['sound']:
some_function(vid,siz,fon,col,lan,crt,sou)