我希望函数的结果是:
这是我的尝试:
>>> 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
Run Code Online (Sandbox Code Playgroud)
[奖金]
该功能应该命名为什么?
Ign*_*ams 24
def unanimous(it):
it1, it2 = itertools.tee(it)
return all(it1) or not any(it2)
Run Code Online (Sandbox Code Playgroud)
dka*_*ins 11
def all_bools_equal(lst):
return all(lst) or not any(lst)
Run Code Online (Sandbox Code Playgroud)
请参阅:http://docs.python.org/library/functions.html#all
请参阅:http://docs.python.org/library/functions.html#any
| 归档时间: |
|
| 查看次数: |
5077 次 |
| 最近记录: |