詹惠儿

2018-11-06   阅读量: 732

数据分析师 Python数据分析 Python编程

如何理解Python中的Any和All

扫码加入数据分析学习群

Any和All是用于连续和/或的python中提供的两个内置命令。

Any
如果任何项为True,则返回true。如果为空或全部为假,则返回False。任何可以被认为是对所提供的迭代的OR运算序列。
它会使执行短路,即在结果已知时立即停止执行。
语法:any(iterables列表)

# Since all are false, false is returned

print (any([False, False, False, False]))

# Here the method will short-circuit at the

# second item (True) and will return True.

print (any([False, True, False, False]))

# Here the method will short-circuit at the

# first (True) and will return True.

print (any([True, False, False, False]))

输出:

False
True
True

All

如果所有项都为True(或者iterable为空),则返回true。所有这些都可以被认为是对所提供的迭代的一系列AND运算。它还会使执行短路,即一旦结果已知就停止执行。

语法:all(iterables列表)

# Here all the iterables are True so all
# will return True and the same will be printed
print (all([True, True, True, True]))

# Here the method will short-circuit at the 
# first item (False) and will return False.
print (all([False, True, True, False]))

# This statement will return False, as no
# True is found in the iterables
print (all([False, False, False]))

输出 :

True
False
False
316.1968 2 5 关注作者 收藏

评论(0)


暂无数据

推荐课程

推荐帖子