简体   繁体   English

如果键匹配,如何从另一个字典附加到字典值

[英]how to append to a dictionary values from another dictionary if keys match

let's say I have two dictionaries假设我有两本词典

dict_1 ={'A': 'a', 'B':'b', 'C': 'c', 'D':'d', 'E':'e','F':f}
dict_2 ={'A': None, 'G': None, 'H': None, 'I': None,'L': None}

I'd like a function that returns the second dictionary as我想要一个返回第二个字典的函数

dict_2 ={'A': 'a', 'G': None, 'H': None, 'I': None,'L': None}

which is matching the keys of dict_1 against those in dict_2.它将 dict_1 的键与 dict_2 中的键匹配。 If one matches replace the value in dict_2 with the value in dict_1 for that key.如果匹配,则将 dict_2 中的值替换为该键的 dict_1 中的值。 Otherwise nothing.否则什么都没有。

A simple way to do this by iterating over dict_2 's items and using dict_1.get() providing default value as dict_2 corresponding value -通过迭代dict_2的项目并使用dict_1.get()提供默认值作为dict_2对应值来执行此操作的简单方法 -

>>> dict_1 ={'A': 'a', 'B':'b', 'C': 'c', 'D':'d', 'E':'e','F':f}
>>> dict_2 ={'A': None, 'G': None, 'H': None, 'I': None,'L': None}
>>> for k,v in dict_2.items():
...     dict_2[k] = dict_1.get(k,v)
...
>>> dict_2
{'G': None, 'H': None, 'I': None, 'L': None, 'A': 'a'}

Same using dict comprehension -同样使用字典理解 -

>>> dict_1 ={'A': 'a', 'B':'b', 'C': 'c', 'D':'d', 'E':'e','F':f}
>>> dict_2 ={'A': None, 'G': None, 'H': None, 'I': None,'L': None}
>>> dict_2 = {k:dict_1.get(k,v) for k,v in dict_2.items()}
>>> dict_2
{'G': None, 'H': None, 'I': None, 'L': None, 'A': 'a'}

Another way is to find the common keys, and iterate over them like this:另一种方法是找到公共键,并像这样迭代它们:

dict_1 ={'A': 'a', 'B':'b', 'C': 'c', 'D':'d', 'E':'e','F':f}
dict_2 ={'A': None, 'G': None, 'H': None, 'I': None,'L': None}

for key in set(dict_1.iterkeys()) & set(dict_2.iterkeys()):
    dict_2[key] = dict_1[key]

This should be much less computational expensive if it's relatively few common entries compared to the total number of entries in the dictionaries.如果与字典中的条目总数相比,公共条目相对较少,则计算成本应该低得多。

You could use dict comprehension and if else to do it :您可以使用 dict comprehension 和if else来执行此操作:

dict_1 ={'A': 'a', 'B':'b', 'C': 'c', 'D':'d', 'E':'e','F':f}
dict_2 ={'A': None, 'G': None, 'H': None, 'I': None,'L': None}
dict_2={a : dict_1[a] if a in dict_1 else dict_2[a] for a in dict_2.keys() }
print dict_2

output:输出:

{'A': 'a', 'H': None, 'I': None, 'L': None, 'G': None}

But this creates a new dict object但这会创建一个新的 dict 对象

You can use dict.viewkeys to find the common keys:您可以使用dict.viewkeys来查找常用键:

dict_1 ={'A': 'a', 'B':'b', 'C': 'c', 'D':'d', 'E':'e','F':'f'}
dict_2 ={'A': None, 'G': None, 'H': None, 'I': None,'L': None}

for k in dict_1.viewkeys() & dict_2.viewkeys():
    dict_2[k] = dict_1[k]

print(dict_2)
{'A': 'a', 'H': None, 'I': None, 'L': None, 'G': None}

For python3 just use .keys as it returns a dictionary-view-object not a list:对于 python3,只需使用.keys因为它返回一个字典视图对象而不是列表:

for k in dict_1.keys() & dict_2.keys():
    dict_2[k] = dict_1[k]

print(dict_2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM