Chinaunix
标题:
all(),any()的矛盾
[打印本页]
作者:
mihello
时间:
2012-10-09 10:13
标题:
all(),any()的矛盾
本帖最后由 mihello 于 2012-10-09 10:14 编辑
>>> all([])
True
>>> help(all)
Help on built-in function all in module builtins:
all(...)
all(iterable) -> bool
Return True if bool(x) is True for all values x in the iterable.
>>> any([])
False
>>> help(any)
Help on built-in function any in module builtins:
any(...)
any(iterable) -> bool
Return True if bool(x) is True for any x in the iterable.
复制代码
空列表的all居然为真,而any为假这个正确。
这为毛了??求解
作者:
darkn3ss
时间:
2012-10-09 17:02
http://stackoverflow.com/questio ... for-empty-iterables
以前还没深究过,这个问题挺好
作者:
linux_c_py_php
时间:
2012-10-11 22:38
没有原因, 只有手册:
all(iterable)
Return True if all elements of the iterable are true (or if the iterable is empty). Equivalent to:
def all(iterable):
for element in iterable:
if not element:
return False
return True
any(iterable)
Return True if any element of the iterable is true. If the iterable is empty, return False. Equivalent to:
def any(iterable):
for element in iterable:
if element:
return True
return False
复制代码
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2