热线电话:13121318867

登录
2018-12-03 阅读量: 805
如何区分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]))

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]))

0.0000
4
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子