什麼是Python的方式來測試集合中的所有元素是否滿足條件? (該.NET All()
method在C#中很好地滿足這一市場。)相當於Python的LINQ所有函數?
有明顯的循環方法:
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
我們有了成爲更優雅的東西......我錯過了什麼?
參見http://stackoverflow.com/questions/8641008/compare-multiple-variables-to-the-same-value-in-if-in-python – 2011-12-27 06:48:21