Python 将两个具有相同键但不同字典作为值的嵌套字典组合起来
我有两个字典(参见代码示例),其中嵌套的字典作为值。我希望连接两个词典,以便在嵌套的词典中获得一个具有附加键值对的词典 我当前的代码可以工作,但在我看来并不枯燥(不要重复)。解决这个问题的最简单的方法是什么Python 将两个具有相同键但不同字典作为值的嵌套字典组合起来,python,dictionary,dry,Python,Dictionary,Dry,我有两个字典(参见代码示例),其中嵌套的字典作为值。我希望连接两个词典,以便在嵌套的词典中获得一个具有附加键值对的词典 我当前的代码可以工作,但在我看来并不枯燥(不要重复)。解决这个问题的最简单的方法是什么 dictionary_base = { 'anton': { 'name': 'Anton', 'age': 29, }, 'bella': { 'name': 'Bella', 'age': 21, }, } dictionary_exte
dictionary_base = {
'anton': {
'name': 'Anton',
'age': 29,
},
'bella': {
'name': 'Bella',
'age': 21,
},
}
dictionary_extension = {
'anton': {
'job': 'doctor',
'address': '12120 New York',
},
'bella': {
'job': 'lawyer',
'address': '13413 Washington',
},
}
for person in dictionary_base:
dictionary_base[person]['job'] = dictionary_extension[person]['job']
dictionary_base[person]['address'] = dictionary_extension[person]['address']
print(dictionary_base)
期望的输出应该是
{'anton': {'address': '12120 New York',
'age': 29,
'job': 'doctor',
'name': 'Anton'},
'bella': {'address': '13413 Washington',
'age': 21,
'job': 'lawyer',
'name': 'Bella'}}
使用
dict.update
Ex:
dictionary_base = {
'anton': {
'name': 'Anton',
'age': 29,
},
'bella': {
'name': 'Bella',
'age': 21,
},
}
dictionary_extenstion = {
'anton': {
'job': 'doctor',
'address': '12120 New York',
},
'bella': {
'job': 'lawyer',
'address': '13413 Washington',
},
}
for person in dictionary_base:
dictionary_base[person].update(dictionary_extenstion[person])
print(dictionary_base)
{'anton': {'address': '12120 New York',
'age': 29,
'job': 'doctor',
'name': 'Anton'},
'bella': {'address': '13413 Washington',
'age': 21,
'job': 'lawyer',
'name': 'Bella'}}
输出:
dictionary_base = {
'anton': {
'name': 'Anton',
'age': 29,
},
'bella': {
'name': 'Bella',
'age': 21,
},
}
dictionary_extenstion = {
'anton': {
'job': 'doctor',
'address': '12120 New York',
},
'bella': {
'job': 'lawyer',
'address': '13413 Washington',
},
}
for person in dictionary_base:
dictionary_base[person].update(dictionary_extenstion[person])
print(dictionary_base)
{'anton': {'address': '12120 New York',
'age': 29,
'job': 'doctor',
'name': 'Anton'},
'bella': {'address': '13413 Washington',
'age': 21,
'job': 'lawyer',
'name': 'Bella'}}
使用
dict.update
Ex:
dictionary_base = {
'anton': {
'name': 'Anton',
'age': 29,
},
'bella': {
'name': 'Bella',
'age': 21,
},
}
dictionary_extenstion = {
'anton': {
'job': 'doctor',
'address': '12120 New York',
},
'bella': {
'job': 'lawyer',
'address': '13413 Washington',
},
}
for person in dictionary_base:
dictionary_base[person].update(dictionary_extenstion[person])
print(dictionary_base)
{'anton': {'address': '12120 New York',
'age': 29,
'job': 'doctor',
'name': 'Anton'},
'bella': {'address': '13413 Washington',
'age': 21,
'job': 'lawyer',
'name': 'Bella'}}
输出:
dictionary_base = {
'anton': {
'name': 'Anton',
'age': 29,
},
'bella': {
'name': 'Bella',
'age': 21,
},
}
dictionary_extenstion = {
'anton': {
'job': 'doctor',
'address': '12120 New York',
},
'bella': {
'job': 'lawyer',
'address': '13413 Washington',
},
}
for person in dictionary_base:
dictionary_base[person].update(dictionary_extenstion[person])
print(dictionary_base)
{'anton': {'address': '12120 New York',
'age': 29,
'job': 'doctor',
'name': 'Anton'},
'bella': {'address': '13413 Washington',
'age': 21,
'job': 'lawyer',
'name': 'Bella'}}
您可以使用字典理解:
{k: {**dictionary_base[k], **dictionary_extension[k]} for k in dictionary_base}
输出:
{'anton': {'name': 'Anton',
'age': 29,
'job': 'doctor',
'address': '12120 New York'},
'bella': {'name': 'Bella',
'age': 21,
'job': 'lawyer',
'address': '13413 Washington'}}
您可以使用字典理解:
{k: {**dictionary_base[k], **dictionary_extension[k]} for k in dictionary_base}
输出:
{'anton': {'name': 'Anton',
'age': 29,
'job': 'doctor',
'address': '12120 New York'},
'bella': {'name': 'Bella',
'age': 21,
'job': 'lawyer',
'address': '13413 Washington'}}
结果应该是什么样的?结果应该是什么样的?谢谢你,这正是我想要的。如果我们能简单地编写“dictionary\u base=dictionary\u base+dictionary\u extension”,那就太好了,但这不起作用:D.谢谢你,这正是我想要的。如果我们能简单地写“dictionary\u base=dictionary\u base+dictionary\u extension”,那就太好了,但那是行不通的:D.谢谢你的回答,从来没有听说过字典理解!你可以看一看《谢谢你的回答,从未听说过字典理解》!你可以看一下桌子