python newbie here. I’m writing the code to control an experiment that has multiple variables. The variables are defined in a csv file something like:
Temp, Voltage, Pressure
23, 4.8, 19
23;28, 4.8, 19;23
I read the file and create a dictionary, that part works ok. for example the first line will be:
{'Temp': ['23'], 'Voltage': ['4.8'], 'Pressure': ['19']}
and that will control a single experiment.
The second line will be:
{'Temp': ['30', '28'], 'Voltage': ['4.8'], 'Pressure': ['19', '23']}
What I would like is a way to generate all the possible dictionaries, in sequence, from that line. For example the second line will be expanded to four experiments:
{'Temp': ['30'], 'Voltage': ['4.8'], 'Pressure': ['19']}
{'Temp': ['30'], 'Voltage': ['4.8'], 'Pressure': ['23']}
{'Temp': ['28'], 'Voltage': ['4.8'], 'Pressure': ['19']}
{'Temp': ['28'], 'Voltage': ['4.8'], 'Pressure': ['23']}
The order is important (the last column is the inner loop). I have a list with the keys in the order I want to iterate. For example:
('Temp', 'Voltage', 'Pressure')
in this case.
This is an oversimplification and there is a big number of variables (columns) in the system.
I’m thinking about a parallel dictionary with counters to keep track of the possible cases and current case for each variable, but maybe I’m missing a more elegant way to express this in python.
Regards,
–ga
Yup, you want
itertools.product(). You just need to extract the lists from the dict & re-wrap the results with your dictionary structure.