简体   繁体   中英

comparing dictionary values with same keys

Is there a clean, pythonic way to do the following?

  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

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.

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.

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