Pythonic way to check if: all elements evaluate to False -OR- all elements evaluate to True

前端 未结 7 737
無奈伤痛
無奈伤痛 2021-02-02 17:33

I want the results of the function to be:

  • All values evaluate to False (None, 0, empty string) -> True
  • All values evaluate to True -> True
  • Every
相关标签:
7条回答
  • 2021-02-02 18:24

    Just yet another way of doing it, given list l:

    sum([int(bool(x)) for x in l]) in (0, len(l))
    
    >>> a=['',None,0,False]
    >>> b=[1,True,'ffffd']
    >>> c=[0,1,False,True,None]
    >>> for l in (a,b,c):
    ...  print sum([int(bool(x)) for x in l]) in (0, len(l))
    ... 
    True
    True
    False
    
    0 讨论(0)
提交回复
热议问题