【发布时间】:2019-05-15 06:22:33
【问题描述】:
我在 Python 中根据其键和值合并两个字典时遇到问题。我有以下情况:
dictionary_1 = { 1{House: red, index=1} , 2{House: blue, index=2} , 3{House: green, index=3}}
dictionary_2 = { 4{Height: 3, index =3} , 5{Height: 5, index=1} , 6{Height: 6, index=2}
例如在“dictionary_1”中,我有一个大字典,其键是“1”和“2”和“3”,它的值是“{House: red, index=1}”和“{House: blue, index=2}”和“{House: green, index=3}”。 正如你所看到的大字典本身也是字典。同样的逻辑也适用于dictionary_2。
我的目标是比较两个大词典的值:“dictionary_1”和“dictionary_2”。然后,如果两个字典的“索引”项具有相同的值,我想将它们合并在一起,而不重复“索引”项。
因此输出应该是这样的:
dictionary_output = { 1{House: red, index=1, Height:5} , 2{House: blue, index=2, Height:6} , 3{House: green, index=3, Height: 3}}
【问题讨论】:
-
是否可以将您的帖子编辑成有效的python?
标签: python dictionary merge compare key-value-store