Python builtin “all” with generators

后端 未结 5 855
太阳男子
太阳男子 2020-12-18 07:07

I have the following problem with python\'s \"all\" and generators:

G = (a for a in [0,1])
all(list(G))   # returns False - as I expected

B

5条回答
  •  别那么骄傲
    2020-12-18 07:57

    I found out in python 3.2.3 if the value 0 is in the list all() will return False.

    for all() to work you have to avoid having a zero in the iteration list.

    It leads me to believe that zero is used as an end for the iteration.

    Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)] on win32

     print(all([]))          # prints True
     print(all([0]))         # prints False
    
     print(all([2, 3]))      # prints True
     print(all([2, 3, 0]))   # prints False
    

提交回复
热议问题