Automatically generate calculations between different dictionnaries and then show same results between calculations

Viewed 20

I'm starting in Python and I need to make make a code in Python that gives me all possible combinations between different dictionaries (every key only has 1 value), then be able to make calculations based on the different combinations, and then compare the results of the calculations. Let me give an exemple:

The different dictionaries: dict_0: {"A0": 1, "A1": 2} dict_1: {"B0": 2, "B1": 1}

Then make all the results "happen", how?: result0 : A0 and B0 result1: A0 and B1 result2: A1 and B0 result3: A1 and B1

Then I need to be able to make automatically calculations between the values of the keys integrated in the results, for exemple: averageResult0 = sum(result0)/2 averageResult0 = 1.5

averageResult3 = sum(result3)/2 averageResult3 = 1.5

Then, within all the automated calculted results, flag the calculations that have the same results; in this case averageResult0 & averageResult3. How can I code that? I've already did my researches but didn't find anything for this particular case.

In the real code, there will be way more dictionnaries and way more keys per dictionnary.

Thank you in advance, I hope my questions are clear :)

1 Answers

If I understand you correctly your problem has the following steps:

  1. Get all pairs of items from dict_0 and dict_1.
  2. Calculate a function on the values of all these pairs.
  3. Report which pairs led to the same values, given this function.

It seems that the function you have is average of the two values.

For step 1. Python has a built-in called itertools.product that does just that. Then you can loop over the pairs and do your calculation easily as step 2.

For step 3 the simplest thing is to use a dict mapping the results to the keys that generated those results. So you want the values to be empty lists that you fill with key pairs whenever you come across a new result. Fortunately, there's a built-in for that too collections.defaultdict which is a dict that automatically creates values of a given type when new keys are added.

from itertools import product
from collections import defaultdict 
dict_0={"A0": 1, "A1": 2} 
dict_1={"B0": 2, "B1": 1}

results = defaultdict(list)
def pair_fxn(x, y):
    return (x + y)/2
for [k0,v0], [k1,v1] in product(dict_0.items(), dict_1.items()):
    results[ pair_fxn(v0, v1) ].append( (k0, k1))

Now you have a dict mapping the results you have calculated to the key pairs that generated them. I'm not sure what "flag" means to you, but you could get a list of key pairs that have results that converge by just filtering:

[(k,v) for k,v in results if len(v)>1] 
# [(1.5, [('A0', 'B0'), ('A1', 'B1')])]

You say there are more dictionaries and more keys in the real problem. This solution assumes that the calculations will always be pairwise. You could extend it to non-pairwise as well but you would have to be clear about what your requirements are for that case.

Related