pythondictionarynestednormalizationstandardization

Normalization of a nested dictionary in python


I am new to Python and I have a nested dictionary for which I want to normalize the values of the dictionary. For example:

nested_dictionary={'D': {'D': '0.33', 'B': '0.17', 'C': '0.00', 'A': '0.17', 'K': '0.00', 'J': '0.03'}, 'A': {'A': '0.50', 'K': '0.00', 'J': '0.08'}}

And I would like to get the normalization as

Normalized_result={'D': {'D': '0.47', 'B': '0.24', 'C': '0.00', 'A': '0.24', 'K': '0.00', 'J': '0.04'}, 'A': {'A': '0.86', 'K': '0.00', 'J': '0.14'}}

I have seen the example in Normalizing dictionary values which only for one dictionary but I want to go further with nested one. I have tried to flatten the nested_dictionary and apply the normalization as

import flatdict
d =  flatdict.FlatDict(nested_dictionary, delimiter='_')
dd=dict(d)
newDict = dict(zip(dd.keys(), [float(value) for value in dd.values()]))

def normalize(d, target=1.0):
    global factor
    raw = sum(d.values())
    print(raw)
    if raw==0:
        factor=0
        #print('ok')
    else:
       # print('kok')
        factor = target/raw
    return {key:value*factor for key,value in d.items()}

normalize(newDict)

And I get the result as

{'D_D': 0.2578125,
 'D_B': 0.1328125,
 'D_C': 0.0,
 'D_A': 0.1328125,
 'D_K': 0.0,
 'D_J': 0.023437499999999997,
 'A_A': 0.39062499999999994,
 'A_K': 0.0,
 'A_J': 0.06249999999999999}

But what I want is the Normalized_result as above Thanks in advance.


Solution

  • This code would do:

    def normalize(d, target=1.0):
        raw = sum(float(number) for number in d.values())
        factor = (target/raw if raw else 0)
        return {key: f'{float(value)*factor:.2f}' for key, value in d.items()}
    
    
    {key: normalize(dct) for key, dct in nested_dictionary.items()}