简体   繁体   English

比较具有相同键的字典值

[英]comparing dictionary values with same keys

Is there a clean, pythonic way to do the following? 有一种干净的,pythonic的方法可以执行以下操作吗?

  1. compare all values from one dictionary to another without iterating over keys and comparing values? 比较一个字典与另一个字典中的所有值,而无需遍历键并比较值?

I'm thinking either a list comprehension or a use of all() would play into this 我想要么列表理解或使用all()都会对此起作用

right now I'm thinking that 现在我在想

for key in dict:
   if dict[key] > otherDict[key]
       return False
return True

any ideas? 有任何想法吗?

给定两个字典dict1dict2 ,可以将all()与生成器结合使用:

all(v <= dict2.get(k) for k, v in dict1.iteritems())
for key in dict:
   if dict[key] > otherDict[key]:
       return False
return True

is the same as 是相同的

return not any(dict[key] > otherDict[key] for key in dict)

any() stops as soon as a true value is found which not then complements. 只要找到一个真值, any()停止,然后not进行补码。

In a function context 在函数上下文中

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

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