Home > Back-end >  Nested for loop for n number
Nested for loop for n number

Time:10-12

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?

CodePudding user response:

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}

CodePudding user response:

You can create a list containing all dictionaries:

lst_tot = [Dic1, Dic2, Dic3, Dic4]

First for the variable sets you can do:

sets = set(val for dic in lst_tot for val in dic.keys())

Then for your for-loops, I found out that you want to multiply the values of the key when the key is present in all dictionaries. You only need to loop over all possible keys, these are computed in sets. To verify if the key is available in all dictionaries you can do:

all([key in dic for dic in lst_tot])

Now to multiply all values use math.prod(list) and you get in total:

import math

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}

lst_tot = [Dic1, Dic2, Dic3, Dic4]

sets = set(val for dic in lst_tot for val in dic.keys())
print(sets)
Result = dict.fromkeys(sets, 0)

## Combination process
for key in sets:
    if all([key in dic for dic in lst_tot]):
        Result[key]  = math.prod([dic[key] for dic in lst_tot])

f = sum(list(Result.values()))
print(f)
for i in Result.keys():
  Result[i] /= f
print(Result)
  • Related