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

前端 未结 7 724
無奈伤痛
無奈伤痛 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:10

    Not so brief, but shortcuts without messing around with 'tee' or anything like that.

    def unanimous(s):
       s = iter(s)
       if s.next():
           return all(s)
       else:
           return not any(s)
    

提交回复
热议问题