[英]comparing dictionary values with same keys
有一種干凈的,pythonic的方法可以執行以下操作嗎?
我想要么列表理解或使用all()都會對此起作用
現在我在想
for key in dict:
if dict[key] > otherDict[key]
return False
return True
有任何想法嗎?
給定兩個字典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
是相同的
return not any(dict[key] > otherDict[key] for key in dict)
只要找到一個真值, any()
停止,然后not
進行補碼。
在函數上下文中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.