Pythonic方法检查是否:所有元素求值为False或所有元素求值为Tru

2024-04-18 09:12:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望函数的结果是:

  • 所有值的计算结果均为False(无,0,空字符串)->;True
  • 所有值的计算结果均为True->;True
  • 每隔一个案例->;错误

这是我的尝试:

>>> def consistent(x):
...  x_filtered = filter(None, x)
...  return len(x_filtered) in (0, len(x))
...
>>> consistent((0,1))
False
>>> consistent((1,1))
True
>>> consistent((0,0))
True

[奖金]

这个函数应该命名为什么?


Tags: 函数字符串gtnonefalsetruelenreturn