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

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

    def all_equals(xs):
        x0 = next(iter(xs), False)
        return all(bool(x) == bool(x0) for x in xs)
    

提交回复
热议问题