I have two lists t1 = ['C1', 'C2', 'C3'] t2 = ['raising', 'falling', 'constant']
using two lists I want output as follows. with all the possible combinations
{
'regime_1' : {
'c1' : 'rising',
'c2' : 'falling',
'c3' : 'constant'
},
'regime_2' : {
'c1' : 'rising',
'c2' : 'falling',
'c3' : 'falling'
}
}
Elements in t1 list should not be repeated inside "regime" key. I have wrote the following code in python.
import itertools
t1 = ['C1', 'C2', 'C3']
t2 = ['raising', 'faling', 'constant']
combos = [str(i) ":" str(n) for i in t1 for n in t2]
combinations = dict(("regime_" str(i 1),c) for i,c in enumerate(combos))
but the out put I get is
{'regime_1': 'C1:raising', 'regime_2': 'C1:faling', 'regime_3': 'C1:constant', 'regime_4': 'C2:raising', 'regime_5': 'C2:faling', 'regime_6': 'C2:constant', 'regime_7': 'C3:raising', 'regime_8': 'C3:faling', 'regime_9': 'C3:constant'}
Thank you
CodePudding user response:
I think this is what you're after:
from itertools import product
variables = ['C1', 'C2', 'C3']
values = ['rising', 'falling', 'constant']
d = {f'regime_{k 1}': dict(v)
for k, v in enumerate(
zip(variables, v) for v in product(values, repeat=len(variables))
)}
print(d)
Some explanation:
product(values, repeat=len(variables))
gives you the cartesian product ofvalues
, with the number of repeats equal to the number of variables.zip()
pairs up variables with their generated values.- By enumerating them, you get the number for the regime together with a set of variables and their values for the
dict
of the regime. - The f-string takes the number from the enumeration ( 1) and creates a key, a dictionary is created from the zip object.
Result:
{
'regime_1': {'C1': 'rising', 'C2': 'rising', 'C3': 'rising'},
'regime_2': {'C1': 'rising', 'C2': 'rising', 'C3': 'falling'},
'regime_3': {'C1': 'rising', 'C2': 'rising', 'C3': 'constant'},
'regime_4': {'C1': 'rising', 'C2': 'falling', 'C3': 'rising'},
'regime_5': {'C1': 'rising', 'C2': 'falling', 'C3': 'falling'},
'regime_6': {'C1': 'rising', 'C2': 'falling', 'C3': 'constant'},
'regime_7': {'C1': 'rising', 'C2': 'constant', 'C3': 'rising'},
'regime_8': {'C1': 'rising', 'C2': 'constant', 'C3': 'falling'},
'regime_9': {'C1': 'rising', 'C2': 'constant', 'C3': 'constant'},
'regime_10': {'C1': 'falling', 'C2': 'rising', 'C3': 'rising'},
'regime_11': {'C1': 'falling', 'C2': 'rising', 'C3': 'falling'},
'regime_12': {'C1': 'falling', 'C2': 'rising', 'C3': 'constant'},
'regime_13': {'C1': 'falling', 'C2': 'falling', 'C3': 'rising'},
'regime_14': {'C1': 'falling', 'C2': 'falling', 'C3': 'falling'},
'regime_15': {'C1': 'falling', 'C2': 'falling', 'C3': 'constant'},
'regime_16': {'C1': 'falling', 'C2': 'constant', 'C3': 'rising'},
'regime_17': {'C1': 'falling', 'C2': 'constant', 'C3': 'falling'},
'regime_18': {'C1': 'falling', 'C2': 'constant', 'C3': 'constant'},
'regime_19': {'C1': 'constant', 'C2': 'rising', 'C3': 'rising'},
'regime_20': {'C1': 'constant', 'C2': 'rising', 'C3': 'falling'},
'regime_21': {'C1': 'constant', 'C2': 'rising', 'C3': 'constant'},
'regime_22': {'C1': 'constant', 'C2': 'falling', 'C3': 'rising'},
'regime_23': {'C1': 'constant', 'C2': 'falling', 'C3': 'falling'},
'regime_24': {'C1': 'constant', 'C2': 'falling', 'C3': 'constant'},
'regime_25': {'C1': 'constant', 'C2': 'constant', 'C3': 'rising'},
'regime_26': {'C1': 'constant', 'C2': 'constant', 'C3': 'falling'},
'regime_27': {'C1': 'constant', 'C2': 'constant', 'C3': 'constant'}
}
CodePudding user response:
import re
import itertools
t1 = ['C1', 'C2', 'C3']
t2 = ['raising', 'faling', 'constant']
variant=0
return_dict=dict()
for current_tag1 in itertools.product(t2, repeat=len(t2)):
return_dict['regime_%d' % variant]=dict(zip(t1,current_tag1))
variant =1
print return_dict
output
{
'regime_18': {'C3': 'raising', 'C2': 'raising', 'C1': 'constant'},
'regime_19': {'C3': 'faling', 'C2': 'raising', 'C1': 'constant'},
'regime_16': {'C3': 'faling', 'C2': 'constant', 'C1': 'faling'},
'regime_17': {'C3': 'constant', 'C2': 'constant', 'C1': 'faling'},
'regime_14': {'C3': 'constant', 'C2': 'faling', 'C1': 'faling'},
'regime_15': {'C3': 'raising', 'C2': 'constant', 'C1': 'faling'},
'regime_12': {'C3': 'raising', 'C2': 'faling', 'C1': 'faling'},
'regime_13': {'C3': 'faling', 'C2': 'faling', 'C1': 'faling'},
'regime_10': {'C3': 'faling', 'C2': 'raising', 'C1': 'faling'},
'regime_11': {'C3': 'constant', 'C2': 'raising', 'C1': 'faling'},
'regime_4': {'C3': 'faling', 'C2': 'faling', 'C1': 'raising'},
'regime_5': {'C3': 'constant', 'C2': 'faling', 'C1': 'raising'},
'regime_6': {'C3': 'raising', 'C2': 'constant', 'C1': 'raising'},
'regime_7': {'C3': 'faling', 'C2': 'constant', 'C1': 'raising'},
'regime_0': {'C3': 'raising', 'C2': 'raising', 'C1': 'raising'},
'regime_1': {'C3': 'faling', 'C2': 'raising', 'C1': 'raising'},
'regime_2': {'C3': 'constant', 'C2': 'raising', 'C1': 'raising'},
'regime_3': {'C3': 'raising', 'C2': 'faling', 'C1': 'raising'},
'regime_8': {'C3': 'constant', 'C2': 'constant', 'C1': 'raising'},
'regime_9': {'C3': 'raising', 'C2': 'raising', 'C1': 'faling'},
'regime_23': {'C3': 'constant', 'C2': 'faling', 'C1': 'constant'},
'regime_22': {'C3': 'faling', 'C2': 'faling', 'C1': 'constant'},
'regime_21': {'C3': 'raising', 'C2': 'faling', 'C1': 'constant'},
'regime_20': {'C3': 'constant', 'C2': 'raising', 'C1': 'constant'},
'regime_26': {'C3': 'constant', 'C2': 'constant', 'C1': 'constant'},
'regime_25': {'C3': 'faling', 'C2': 'constant', 'C1': 'constant'},
'regime_24': {'C3': 'raising', 'C2': 'constant', 'C1': 'constant'}
}
CodePudding user response:
from itertools import product, zip_longest
t1 = ['C1', 'C2', 'C3']
t2 = ['raising', 'falling', 'constant']
t2 = product(t2,repeat=len(t1))
result = {f"regime_{idx}":dict(zip(*value)) for idx,value in enumerate(zip_longest([t1],t2,fillvalue=t1),1)}
print(result)
{'regime_1': {'C1': 'raising', 'C2': 'raising', 'C3': 'raising'},
'regime_2': {'C1': 'raising', 'C2': 'raising', 'C3': 'falling'},
'regime_3': {'C1': 'raising', 'C2': 'raising', 'C3': 'constant'},
'regime_4': {'C1': 'raising', 'C2': 'falling', 'C3': 'raising'},
'regime_5': {'C1': 'raising', 'C2': 'falling', 'C3': 'falling'},
'regime_6': {'C1': 'raising', 'C2': 'falling', 'C3': 'constant'},
'regime_7': {'C1': 'raising', 'C2': 'constant', 'C3': 'raising'},
'regime_8': {'C1': 'raising', 'C2': 'constant', 'C3': 'falling'},
'regime_9': {'C1': 'raising', 'C2': 'constant', 'C3': 'constant'},
'regime_10': {'C1': 'falling', 'C2': 'raising', 'C3': 'raising'},
'regime_11': {'C1': 'falling', 'C2': 'raising', 'C3': 'falling'},
'regime_12': {'C1': 'falling', 'C2': 'raising', 'C3': 'constant'},
'regime_13': {'C1': 'falling', 'C2': 'falling', 'C3': 'raising'},
'regime_14': {'C1': 'falling', 'C2': 'falling', 'C3': 'falling'},
'regime_15': {'C1': 'falling', 'C2': 'falling', 'C3': 'constant'},
'regime_16': {'C1': 'falling', 'C2': 'constant', 'C3': 'raising'},
'regime_17': {'C1': 'falling', 'C2': 'constant', 'C3': 'falling'},
'regime_18': {'C1': 'falling', 'C2': 'constant', 'C3': 'constant'},
'regime_19': {'C1': 'constant', 'C2': 'raising', 'C3': 'raising'},
'regime_20': {'C1': 'constant', 'C2': 'raising', 'C3': 'falling'},
'regime_21': {'C1': 'constant', 'C2': 'raising', 'C3': 'constant'},
'regime_22': {'C1': 'constant', 'C2': 'falling', 'C3': 'raising'},
'regime_23': {'C1': 'constant', 'C2': 'falling', 'C3': 'falling'},
'regime_24': {'C1': 'constant', 'C2': 'falling', 'C3': 'constant'},
'regime_25': {'C1': 'constant', 'C2': 'constant', 'C3': 'raising'},
'regime_26': {'C1': 'constant', 'C2': 'constant', 'C3': 'falling'},
'regime_27': {'C1': 'constant', 'C2': 'constant', 'C3': 'constant'}}