Is there a clean, pythonic way to do the following?
I'm thinking either a list comprehension or a use of all() would play into this
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.
In a function context
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.