📜  组合两个字典为公共键添加值 - Python 代码示例

📅  最后修改于: 2022-03-11 14:46:03.278000             🧑  作者: Mango

代码示例1
a = {
    "a": 1,
    "b": 2,
    "c": 3
}

b = {
    "a": 2,
    "b": 3,
    "d": 5
}

# Python 3.5

for key in b:
    if key in a:
        b[key] = b[key] + a[key]

c = {**a, **b}
print(c)

>>> c
{'a': 3, 'b': 5, 'c': 3, 'd': 5}