Search code examples
pythonpython-3.xfor-loopnested-loops

Nested for loop for n number


I have a piece of code for which I need to define a function such that it will work for any number n.

Dic1 = {'a':0.001,'b':0.999,'c':0.0}
Dic2 = {'a':0.018,'b':0.982,'c':0.0}
Dic3 = {'a':0.999,'b':0.001,'c':0.0}
Dic4 = {'a':0.999,'b':0.001,'c':0.0}
sets=set(Dic1.keys()).union(set(Dic2.keys())).union(set(Dic3.keys())).union(set(Dic4.keys()))
print(sets)
Result=dict.fromkeys(sets,0)
## Combination process
for i in Dic1.keys():
    for j in Dic2.keys():
      for k in Dic3.keys():
        for l in Dic4.keys():
          if set(str(i)).intersection(set(str(j))).intersection(set(str(k))).intersection(set(str(l))) == set(str(i)):
              Result[i]+=Dic1[i]*Dic2[j]*Dic3[k]*Dic4[l]
          elif set(str(i)).intersection(set(str(j))).intersection(set(str(k))).intersection(set(str(l))) == set(str(j)):
              Result[j]+=Dic1[i]*Dic2[j]*Dic3[k]*Dic4[l]
          elif set(str(i)).intersection(set(str(j))).intersection(set(str(k))).intersection(set(str(l))) == set(str(k)):
              Result[k]+=Dic1[i]*Dic2[j]*Dic3[k]*Dic4[l]
          elif set(str(i)).intersection(set(str(j))).intersection(set(str(k))).intersection(set(str(l))) == set(str(l)):
              Result[l]+=Dic1[i]*Dic2[j]*Dic3[k]*Dic4[l]
f= sum(list(Result.values()))
print(f)
for i in Result.keys():
  Result[i] /=f
print(Result)

So if I have only 3 dictionaries then the code should be

Dic1 = {'a':0.001,'b':0.999,'c':0.0}
Dic2 = {'a':0.018,'b':0.982,'c':0.0}
Dic3 = {'a':0.999,'b':0.001,'c':0.0}
sets=set(Dic1.keys()).union(set(Dic2.keys())).union(set(Dic3.keys()))
print(sets)
Result=dict.fromkeys(sets,0)
## Combination process
for i in Dic1.keys():
    for j in Dic2.keys():
      for k in Dic3.keys():
          if set(str(i)).intersection(set(str(j))).intersection(set(str(k))) == set(str(i)):
              Result[i]+=Dic1[i]*Dic2[j]*Dic3[k]
          elif set(str(i)).intersection(set(str(j))).intersection(set(str(k))) == set(str(j)):
              Result[j]+=Dic1[i]*Dic2[j]*Dic3[k]
          elif set(str(i)).intersection(set(str(j))).intersection(set(str(k))) == set(str(k)):
              Result[k]+=Dic1[i]*Dic2[j]*Dic3[k]
f= sum(list(Result.values()))
print(f)
for i in Result.keys():
  Result[i] /=f
print(Result)

In short for n dictionaries, the variable sets should also change along with the combination process. How do I proceed with this?


Solution

  • It looks like you are doing whole lot of looping to simply multiply values from similar keys together. Here is an example of how you could do this in pandas, but there are examples on SO where you can multiple multiple dicts together w/o using other libraries.

    In any case, you just need a list of your dicts before proceeding, in this case d

    import pandas as pd
    
    Dic1 = {'a':0.001,'b':0.999,'c':0.0}
    Dic2 = {'a':0.018,'b':0.982,'c':0.0}
    Dic3 = {'a':0.999,'b':0.001,'c':0.0}
    Dic4 = {'a':0.999,'b':0.001,'c':0.0}
    
    d = [Dic1,Dic2,Dic3,Dic4]
    
    df = pd.DataFrame(d).prod()
    df.div(sum(df.values)).to_dict()
    

    Output

    {'a': 0.9482176755958659, 'b': 0.05178232440413414, 'c': 0.0}