简体   繁体   中英

all() returning a generator?

So I want to test if a list is sorted. After reading this page , I did this:

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:]) )

Output

<generator object <genexpr> at 0x10d9ecaa0>

Ok so all() return a generator. But this is what the Python documentation says about all() :

Return True if all elements of the iterable are true (or if the iterable is empty)

What am i missing?

This is a problem of those silly star-imports:

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's all works fine.

You can access it via __builtin__ module in python2 and builtins module in python3:

import __builtin__
__builtin__.all(b >= a for a, b in zip(ll, ll[1:]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM