所以我想测试一个列表是否已排序。看完这篇page ,我这样做了:
ll = [ 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15 ]
all(b >= a for a, b in zip(ll, ll[1:]) )
输出
<generator object <genexpr> at 0x10d9ecaa0>
好的,all()
返回一个生成器。但这是 Python 文档关于 all()
的说法:
Return True if all elements of the iterable are true (or if the iterable is empty)
我错过了什么?
最佳答案
这是那些愚蠢的明星进口的问题:
from numpy import *
ll = [ 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15 ]
all(b >= a for a, b in zip(ll, ll[1:]) )
#>>> <generator object <genexpr> at 0x7f976073fdc0>
Python 的 all
工作正常。
你可以通过python2中的__builtin__
模块和python3中的builtins
模块访问它:
import __builtin__
__builtin__.all(b >= a for a, b in zip(ll, ll[1:]))
关于python - all() 返回一个生成器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21783066/