python 中嵌套字典的规范化

Normalization of a nested dictionary in python

我是 Python 的新手,我有一个嵌套字典,我想为其规范化字典的值。例如:

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'}}

我想将规范化为

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'}}

我看过 Normalizing dictionary values 中的示例,它仅适用于一本字典,但我想进一步了解嵌套字典。 我试图压平 nested_dictionary 并将规范化应用为

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)

我得到的结果是

{'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}

但是我要的是上面的Normalized_result 提前致谢。

  1. 将内部字典中的 string-values 变成浮点数。
  2. duplicate 中的一种解决方案,例如 really_safe_normalise_in_place
  3. 对每个字典使用解决方案。

示例:

d = {'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'}}
d = {k: {kk: float(vv) for kk, vv in v.items()} for k, v in d.items()}
for v in d.values():
    really_safe_normalise_in_place(v)

这段代码可以做到:

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()}
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'}}

在这个例子中,你的dict值是str类型,所以我们需要转换为float:

nested_dictionary = dict([b, dict([a, float(x)] for a, x in y.items())] for b, y in nested_dictionary.items())

nested_dictionary

{'D': {'D': 0.33, 'B': 0.17, 'C': 0.0, 'A': 0.17, 'K': 0.0, 'J': 0.03},
 'A': {'A': 0.5, 'K': 0.0, 'J': 0.08}}

以下函数改编自您提供的link。 它遍历字典,计算因子并更新值。

for _, d in nested_dictionary.items():
    factor = 1.0/sum(d.values())
    for k in d:
        d[k] = d[k] * factor

nested_dictionary

{'D': {'D': 0.47142857142857136,
  'B': 0.24285714285714285,
  'C': 0.0,
  'A': 0.24285714285714285,
  'K': 0.0,
  'J': 0.04285714285714285},
 'A': {'A': 0.8620689655172414, 'K': 0.0, 'J': 0.13793103448275865}}

如果您需要转换回 str,请使用以下函数:

nested_dictionary = dict([b, dict([a, "{:.2f}".format(x)] for a, x in y.items())] for b, y in nested_dictionary.items())

nested_dictionary

{'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'}}