python - all() 返回一个生成器?

标签 python iterator generator

所以我想测试一个列表是否已排序。看完这篇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/

相关文章:

c++11 - unique_ptr 向量的取消引用包装器?

file-io - 在 Common Lisp 中逐行(低内存)读取文件

python - 在倒数第二次迭代中停止生成器的 for 循环

python - 有没有办法记住 python 迭代器中的位置?

php - 为什么 'iterator_to_array' 给出的结果与 foreach 不同?

python - 如何将fig保存为matplotlib.pyplot中的显示?

python - 在 Pandas 中使用滚动的滑动窗口迭代器

python - 为所有 Flask 路由添加前缀

Python2 : Using . decode with errors ='replace' 仍然返回错误

java - 如何在 Java 中设置 PNG 的每平方英寸点数 (DPI)?