如下:
>>> all(())
True
>>> all([])
True
>>> all(1,2,3)
Traceback (most recent call last):File "<stdin>", line 1, in <module>
TypeError: all() takes exactly one argument (3 given)
>>> all([1,2,3])
True
>>> all([1,2,3,])
True
>>> all([1,2,3,''])
False
>>> all([1,2,3,0])
False
>>> all([1,2,3,false])
Traceback (most recent call last):File "<stdin>", line 1, in <module>
NameError: name 'false' is not defined
>>> all([1,2,3,'false'])
True
>>> all([1,2,3,'False'])
True>>> any(())
False
>>> any([])
False
>>> any([1,2,3])
True
>>> any([1,2,3,])
True
>>> any([1,2,3,''])
True
>>> any([1,2,3,0])
True
本质上讲,any()实现了或(OR)运算,而all()实现了与(AND)运算。
对于any(iterables),如果可迭代对象iterables(至于什么是可迭代对象,可关注我的下篇文章)中任意存在每一个元素为True则返回True。特例:若可迭代对象为空,比如空列表[],则返回False。
官方文档如是说:
Return True if any element of the iterable is true. If the iterable is empty, return False.
伪代码(其实是可以运行的python代码,但内置的any是由C写的)实现方式:
def any(iterable):
for element in iterable:
if element:
return True
return False
对于all(iterables),如果可迭代对象iterables中所有元素都为True则返回True。特例:若可迭代对象为空,比如空列表[],则返回True。
官方文档如是说:
Return True if all elements of the iterable are true (or if the iterable is empty).
伪代码(其实是可以运行的python代码,但内置的all是由C写的)实现方式:
def all(iterable):
for element in iterable:
if not element:
return False
return True
转自:https://blog.csdn.net/cython22/article/details/78829288