简体   繁体   English

如何比较来自两个不同字典的相同键值

[英]How to compare same keys values from two different dict

Trying to compare same keys values from two different dict, If second dict value is bigger than first dict value then output should show different keys values only. 尝试比较来自两个不同dict的相同键值,如果第二个dict值大于第一个dict值,则输出应仅显示不同的键值。

Example:
first={'a': '1000', 'b': '2000', 'c': '3000'}
second={'a': '1000', 'b': '3000', 'c': '5000'}
new dict output should be {'b': '3000', 'c': '5000'}

how to do this comperison 如何做这个比较

Using a dict comprehension 使用字典理解

Ex: 例如:

first={'a': '1000', 'b': '2000', 'c': '3000'}
second={'a': '1000', 'b': '3000', 'c': '5000'}
print(dict((k, second[k])for k in second if second[k] > first[k]))

Output: 输出:

{'c': '5000', 'b': '3000'}

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

相关问题 如何从值不同的两个字典中查找公用密钥 - how to find common keys from two dict with difference in values 如何比较两个不同字典中相同键的值 - How to compare values of same keys in two separate dictionaries 如何在python中比较具有相同PCollection的两个键的所有值? - How to compare all the values of two keys with in same PCollection in python? 如何比较具有相同键的两个字典并使用 python 中的条件更新值? - How to compare two dictionaries with the same keys and update values with a condition in python? 如何比较 python 中具有不同键的两个字典的值 - How to compare values of two dictionaries that have different keys in python 如何比较具有不同键但相似值的两个字典并删除重复项 - how to compare two dictionaries with different keys but similar values and delete the duplicates 如何使用两个相同的键和不同的值从 Python 中的三个不同列表创建字典? - How to create dictionary from three different list in Python using two same Keys and different values? 制作字典的无限版本,相同的键,不同的值 - Make unlimited versions of a dict, same keys, different values 如何比较 python 中两个值是否相同但不同的情况 - how to compare if two values are the same but different cases in python 如何通过值从字典中获取关键信息? - How to get top of keys from dict by values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM