简体   繁体   English

比较两个字典的键/值并放入新字典中

[英]Compare the key/values of two dictionaries and put into new dictionary

I know variations on this question already exist, but I cannot find one that matches exactly what I am trying to achieve. 我知道这个问题已经存在,但是我找不到与我要达到的目标完全匹配的问题。 I have the following code, which included a solution I have taken from a solution to a similar question: 我有以下代码,其中包括从类似问题的解决方案中获取的解决方案:

b = {"1":0,"2":0,"3":0,"4":0,"5":0}
c = {"1":1,"4":4,"5":5}

d = [k for k in b if c.get(k, object()) > b[k]]

print d

What I want is to compare all the key and value pairs of the dictionary b with those of c . 我想要的是将字典b所有键和值对与c If a key and value pair is missing from c then the key/pair value of b is retained in the dictionary d , else the values in c are retained in d . 如果一个键和值的对从丢失c然后的键/对值b被保留在字典d ,否则在值c保留在d

In the example above d should look like this: 在上面的示例中, d应该看起来像这样:

d = {"1":1,"2":0,"3":0,"4":4,"5":5}

Can anyone tell me the correct syntax I need for the line d = , please? 谁能告诉我d =行所需的正确语法?

Thanks 谢谢

To answer your actual question 回答您的实际问题

What I want is to compare all the key and value pairs of the dictionary b with those of c. 我想要的是将字典b的所有键和值对与c进行比较。 If a key and value pair is missing from c then the key/pair value of b is retained in the dictionary d, else the values in c are retained in d. 如果c中缺少键和值对,则b的键/对值将保留在字典d中,否则c中的值将保留在d中。

>>> b = {"1":0,"2":0,"3":0,"4":0,"5":0}
>>> c = {"1":1,"4":4,"5":5}
>>> d = {k: c.get(k, b[k]) for k in b}
>>> d
{'1': 1, '3': 0, '2': 0, '5': 5, '4': 4}

In Python 3 you should use collections.ChainMap (note this is slightly different in that it will take any key from c , not just the ones in b ) 在Python 3中,您应该使用collections.ChainMap (请注意,这将略有不同,因为它将使用c任何键,而不仅仅是b

>>> from collections import ChainMap
>>> b = {"1":0,"2":0,"3":0,"4":0,"5":0}
>>> c = {"1":1,"4":4,"5":5}
>>> d = ChainMap(c, b)
>>> d
ChainMap({'1': 1, '5': 5, '4': 4}, {'1': 0, '3': 0, '2': 0, '5': 0, '4': 0})
>>> d['1'], d['2'], d['4']
(1, 0, 4)

You need to use an if-else condition in your comprehension, and also you dont need to use get when every thing is clear : 您需要在理解中使用if-else条件,并且当每件事都清楚时也不需要使用get

>>> d={k:c[k] if k in c and c[k]>b[k] else v for k,v in b.items()}
>>> d
{'1': 1, '3': 0, '2': 0, '5': 5, '4': 4}

Or perhaps this: 也许这样:

b = {"1":0,"2":0,"3":0,"4":0,"5":0}
c = {"1":1,"4":4,"5":5}

d = {k:b[k] if not c.get(k)>b[k] else c[k] for k in b}

d
{'1': 1, '2': 0, '3': 0, '4': 4, '5': 5}

Or, if you want to unpack both key/values from b to compare: 或者,如果要从b解压缩两个键/值以进行比较:

{k:v if not c.get(k)>v else c[k] for k, v in b.iteritems()}

The part of k:v is treated as key=k , and value= v only if k doesn't exist in c and c[k] value is > v , otherwise take v . k的部分:V是视为key=k ,和value= v仅当k不存在cc[k]值是> v ,否则取v

And since c.get(k) returns None if k doesn't exist in c, and None > v will automatically translate to False . 并且由于c.get(k)如果c中不存在k则返回None ,并且None > v将自动转换为False

暂无
暂无

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

相关问题 将两个字典与一个共同的密钥进行比较,并形成一个新的字典,组合与该公共密钥关联的其余密钥 - Compare two dictionaries with one key in common and form a new dictionary combining the remaining keys associated with this common key python词典匹配两个词典中的键值 - python dictionary match key values in two dictionaries 匹配两个字典中的键,并使用匹配的键和两个关联的值制作新词典 - Match keys from two dictionaries, and make new dictionary with matched key and two associated values 仅比较字典列表中的某些字典键值 Python - Compare only certain dictionary key values within list of dictionaries Python 比较两个字典键并使用Python中的列表值创建字典 - Compare two dictionaries keys and create dictionary with list values in Python 根据字典中的两个值比较多个字典以进行输入 - Compare multiple dictionaries for entry based on two values within dictionary Python读取两个字典比较值并创建第三个字典 - Python read two dictionaries compare values and create third dictionary 比较两个字典并将识别的键和值差异添加到新字典 - Compare two dictionaries and add the identified key and difference in value to a new dict 如何匹配两个字典中的统计数据并按新字典中的关键字进行分组? - How to match statistics from two dictionaries and group by key in new dictionary? 比较两个字典的键,并使用与另一个字典中的键不匹配的键、值对创建一个字典 - Python - Compare the keys of two dictionaries and create a dictionary with key, value pairs that does not match the key in the other dictionary - Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM