【发布时间】:2017-05-17 07:59:19
【问题描述】:
我有 2 个字典,例如:
x = {2: {'a':1, 'b':1}}
y = {3: {'a':1}, 2: {'a':2, 'c':2}}
我想要的合并结果应该是:
z = {3: {'a':1}, 2: {'a':3, 'b':1, 'c':2}}
我试过dict(x.items() + y.items())和Counter,但只得到
{2: {'a': 2, 'c': 2}, 3: {'a': 1}}
结果。
如何合并其值也是 dict 本身的 dict?
我已经解释了How to merge two Python dictionaries in a single expression?之间的区别 是不是我的字典的值也是字典。
【问题讨论】:
-
它不是那个的副本。也许它是重复的,但更难找到。好问题。
标签: python dictionary