如何使用相同的主键组合两个嵌套字典
我有两个具有相同主键的嵌套字典:
dict1 = {'person1': {'name': 'John', 'sex': 'Male'},
'person2': {'name': 'Marie', 'sex': 'Female'},
'person3': {'name': 'Luna', 'sex': 'Female'},
'person4': {'name': 'Peter', 'sex': 'Male'}}
dict2 = {'person1': {'weight': '81.1', 'age': '27'},
'person2': {'weight': '56.7', 'age': '22'},
'person3': {'weight': '63.4', 'age': '24'},
'person4': {'weight': '79.1', 'age': '29'}}
所以我想通过 dict2 中的键值对来丰富 dict 1。
我可以用 for 循环来做到这一点...
for key in dict2:
dict2[key]['age'] = dict1[key]['age']
dict2[key]['weight'] = dict2[key]['weight']
结果:
dict2 = {'person1': {'name': 'John', 'sex': 'Male', 'weight': '81.1', 'age': '27'},
'person2': {'name': 'Marie', 'sex': 'Female', 'weight': '56.7', 'age': '22'},
'person3': {'name': 'Luna', 'sex': 'Female', 'weight': '63.4', 'age': '24'},
'person4': {'name': 'Peter', 'sex': 'Male', 'weight': '79.1', 'age': '29'}}
...但是有没有一种更 Pythonic 的方法来做到这一点 - 例如使用 dict 理解?
I have two nested dicts with same master keys:
dict1 = {'person1': {'name': 'John', 'sex': 'Male'},
'person2': {'name': 'Marie', 'sex': 'Female'},
'person3': {'name': 'Luna', 'sex': 'Female'},
'person4': {'name': 'Peter', 'sex': 'Male'}}
dict2 = {'person1': {'weight': '81.1', 'age': '27'},
'person2': {'weight': '56.7', 'age': '22'},
'person3': {'weight': '63.4', 'age': '24'},
'person4': {'weight': '79.1', 'age': '29'}}
So I want to enrich dict 1 by the key value pairs from dict2.
I'm able to do so with a for loop...
for key in dict2:
dict2[key]['age'] = dict1[key]['age']
dict2[key]['weight'] = dict2[key]['weight']
Result:
dict2 = {'person1': {'name': 'John', 'sex': 'Male', 'weight': '81.1', 'age': '27'},
'person2': {'name': 'Marie', 'sex': 'Female', 'weight': '56.7', 'age': '22'},
'person3': {'name': 'Luna', 'sex': 'Female', 'weight': '63.4', 'age': '24'},
'person4': {'name': 'Peter', 'sex': 'Male', 'weight': '79.1', 'age': '29'}}
...but is there a more pythonic way to do so - e.g. with dict comprehension?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的:
首先,使用
.items()
同时迭代键和值。然后,对于每个键
k
,您希望该值是通过转储(或解构)v
和dict2[k]在其中。
Python 更新 >= 3.9:
感谢 @mwo 提到管道
|
操作数:Yes:
Firstly, use
.items()
to iterate over both keys and values at the same time.Then, for each key
k
you want the value to be a new dict that is created by dumping — or destructuring — bothv
anddict2[k]
in it.UPDATE for Python >= 3.9:
Thanks @mwo for mentioning the pipe
|
operand:如果您可以控制数据源,请展平字典,然后使用
update
方法。例如:处理这种数据结构要容易得多,但如果您受困于嵌套字典,您可以使用
NestedDict
通过类似的接口实现相同的结果。如果您需要结果作为字典,请使用 nd1.to_dict() 。
安装ndicts
pip install ndicts
。If you have control over the data source flatten the dictionaries and then use the
update
method. For example:It is much easier to deal with this kind of data structure, but if you are stuck with nested dictionaries you can use a
NestedDict
to achieve the same result with a similar interface.Use
nd1.to_dict()
if you need the result as a dictionary.To install ndicts
pip install ndicts
.答案不考虑要合并的字典中深度超过 2 的级别,
要合并到任意深度,请使用以下递归:
The answers do not consider levels deeper than 2 in the dicts to be merged,
to merge to any depth use the following recursion: