【发布时间】:2011-12-27 04:14:37
【问题描述】:
测试集合中的所有元素是否满足条件的惯用 Python 方法是什么? (.NET All() method 在 C# 中很好地填补了这个空白。)
有明显的循环方法:
all_match = True
for x in stuff:
if not test(x):
all_match = False
break
列表推导可以解决问题,但似乎很浪费:
all_match = len([ False for x in stuff if not test(x) ]) > 0
必须有更优雅的东西......我错过了什么?
【问题讨论】: