【发布时间】:2019-11-17 19:10:37
【问题描述】:
我有两个字典:
d={'doc_1': {'hope': 1, 'court': 2}, 'doc_2': {'hope': 1, 'court': 1}, 'doc_3': {'hope': 1, 'mention': 1}}
和
count={'doc_1': 6, 'doc_2': 5, 'doc_3': 12}
我只想根据两个字典的相同键将字典 d 的嵌套字典的值与字典 count 的值分开。
预期输出:-
new={{'doc_1': {'hope': 0.16666666, 'court': 0.3333333}, 'doc_2': {'hope': 0.2, 'court': 0.2}, 'doc_3': {'hope': 0.0833333, 'mention': 0.0833333}}。
到目前为止我做了什么:
new={}
for k,v in d.items():
for p,q in count.items():
for w,r in v.items():
if k==p:
ratio=r/q
new[k][w]=ratio
这给了我一个错误!!!
【问题讨论】:
标签: arrays python-3.x dictionary