all()和any()是python的内建函数。
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.如果所有的值都为True,则返回True。
If the iterable is empty, return True.注意,如果对象为空,也返回True。
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.只要有一个值为True,则返回True。
If the iterable is empty, return False.注意,如果对象为空,则返回False。
>>> lst3 = []
>>> if all(lst3):
print(1)
1
>>> if lst3:
print(1)
>>> if any(lst3):
print(1)
>>>
>>> a = -1
>>> b = -2
>>> c = 1
>>> d = 2
>>> e = ' '
>>> f = 'haha'
>>> g = False
>>> h = None
>>> i = 0
>>> j = ''
>>> lst1 = [a,b,c,d,e,f,g,h,i,j]
>>> lst2 = [a,b,c,d,e,f]
>>> lst1
[-1, -2, 1, 2, ' ', 'haha', False, None, 0, '']
>>> lst2
[-1, -2, 1, 2, ' ', 'haha']
>>> if not all(lst1):
print('Some of the elements in lst1 are not True')
else:
print('All the elements in lst1 are True')
Some of the elements in lst1 are not True
>>> if not all(lst2):
print('Some of the elements in lst2 are not True')
else:
print('All the elements in lst2 are True')
All the elements in lst2 are True
>>>
>>>
>>> if all(lst1):
print('All the elements in lst1 are True')
else:
print('Not all the elements in lst1 are True')
Not all the elements in lst1 are True
>>> if all(lst2):
print('All the elements in lst2 are True')
else:
print('Not all the elements in lst2 are True')
All the elements in lst2 are True
>>>