作者:戚敦崩 | 来源:互联网 | 2023-01-05 10:51
英文文档:any(iterable)ReturnTrueifanyelementoftheiterableistrue.Iftheiterableisempty,r
英文文档:
any(iterable)
Return True if any element of the iterable is true. If the iterable is empty, return False Equivalent to:
如果iterable的一个元素不为0、”、False或者iterable为空,any(iterable)返回True,否则返回False。函数等价于:
def any(iterable):
for element in iterable:
if element:
return True
return False
any()
1、 接受一个可迭代器对象为参数,当参数为空或者不为可迭代器对象是报错。
>>> any(2) #传入数值报错
Traceback (most recent call last):
File "", line 1, in
any(2)
TypeError: 'int' object is not iterable
2、如果可迭代对象中其中一个元素的逻辑值为True时,返回True,全部值均为False时返回False。
>>> any([0,1,2]) #列表元素有一个为True,则返回True
True
>>> any([0,0]) #列表元素全部为False,则返回False
False
3、如果可迭代对象为空(元素个数为0),返回False。
>>> any([]) #空列表
False
>>> any({}) #空字典
False
小结
希望通过上面的操作能帮助大家。如果你有什么好的意见,建议,或者有不同的看法,我都希望你留言和我们进行交流、讨论。
欢迎关注微信公众号,访问更多精彩:AiryData。
转载请注明:数据之美 » Python3.6内置函数(3)——any()