python 程序员如何检查字典的任何值是否与条件匹配(大于0
我的情况)。我正在寻找对性能影响最小的最“pythonic”的方式。
我的字典:
pairs = { 'word1':0, 'word2':0, 'word3':2000, 'word4':64, 'word5':0, 'wordn':8 }
到目前为止,我使用了这两种(可怕的?)方法。
1:
options = pairs.values() # extract values
for i in options:
if i > 0:
return True
return False
2:
options = sorted(pairs.items(), key=lambda e: e[1], reverse=True) # rank from max to min
if options[0][1] > 0:
return True
else:
return False