Python 中的 all() 和 any()

Python 中的 all() 和 any()

转载请注明出处:https://blog.csdn.net/jpch89/article/details/85119026



1. all()

交互模式下使用 help(all) 查看帮助文档:

>>> help(all)
Help on built-in function all in module builtins:

all(iterable, /)
    Return True if bool(x) is True for all values x in the iterable.
    
    If the iterable is empty, return True.
  • all() 函数接收一个可迭代对象作为参数,不能不传参数调用。
  • 对可迭代对象中的每个元素 x 都进行布尔类型转换 bool(x),如果它们都为 True,那么返回 True,只要有一个为 False,则返回 False
  • 假如可迭代对象为空,返回 True

相当于 x1 and x2 and x3 ... and xn 得到的结果


2. any()

交互模式下使用 help(any) 查看帮助文档:

>>> help(any)
Help on built-in function any in module builtins:

any(iterable, /)
    Return True if bool(x) is True for any x in the iterable.
    
    If the iterable is empty, return False.

  • any() 函数接收一个可迭代对象作为参数,不能不传参数调用。
  • 对可迭代对象中的每个元素 x 都进行布尔类型转换 bool(x),如果它们都为 False,那么返回 False,只要有一个为 True,则返回 True
  • 假如可迭代对象为空,返回 False

相当于 x1 or x2 or x3 ... or xn 得到的结果


完成于 2018.12.27

猜你喜欢

转载自blog.csdn.net/jpch89/article/details/85119026