我想要的功能的结果是:Python的方式来检查:所有元素评估为False - 或 - 所有元素评估为True
- 所有值评估为假(无,0,空字符串) - >真
- 所有值评估为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
[奖金]
该函数应该命名为什么?
打我了 - 我决定先测试它(和正要给我们一个lambda – Gerrat 2010-11-30 05:17:42
...哎呦没有看到奖金 – Gerrat 2010-11-30 05:18:08
@Gerrat - 只要有StackOverflow的Python解释器建设成答案框;-) – dkamins 2010-11-30 05:20:21