[英]comparing dictionary values with same keys
Is there a clean, pythonic way to do the following? 有一种干净的,pythonic的方法可以执行以下操作吗?
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? 有任何想法吗?
给定两个字典dict1
和dict2
,可以将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.