【发布时间】:2017-10-13 16:12:53
【问题描述】:
我希望合并下面显示的两个字典,但没有成功。
我已经阅读了很多博客文章,但我没有找到答案。
dict1={"KPNS": {"metadocdep": {"eta": {"sal": "2"}}, "metadocdisp": {"meta": {"head": "1"}}}, "EGLS": {"apns": {"eta": {"sal": "2"}}, "gcm": {"meta": {"head": "1"}}}}
dict2={"KPNS": {"metadocdep": {"eta": {"sal": "7"}}, "metadocdisp": {"meta": {"head": "5"}}}, "EGLS": {"apns": {"eta": {"sal": "7"}}, "gcm": {"meta": {"head": "9"}}}}
finaldict = {key:(dict1[key], dict2[key]) for key in dict1}
print finaldict
我的最终输出应该是这样的:
{"KPNS": {"metadocdep": {"eta": {"sal": [2,7]}},
"metadocdisp": {"meta": {"head": [1,5]}}},
"EGLS": {"apns": {"eta": {"sal": [2,7]}},
"gcm": {"meta": {"head": [1,9]}}}}
我该怎么做?
【问题讨论】:
-
您的 dicts 是否具有恒定的深度或它们具有不确定的 subdicts?
-
因为你必须为你的字典定义一个结构。有超过4级吗?只有最后一层必须是列表?我们可以在每个级别的两个字典中有不同的元素吗?
-
它们有恒定的深度
-
@AdouaniRiadh 只有最后一层是列表。每个级别没有不同的元素
标签: python dictionary merge