Any (iterable)
All (iterable)
Any () differs from the all () function, any is arbitrary, and all is all.
Version: This function is suitable for version 2.5 and above, compatible with Python3 version.
Any (iterable) Description: Parameter iterable: can iterate object;
If all values of iterable are 0, ' ' or false, then the result is false, and if there is a value in all elements that is not 0, ' ' or false, the result is true
The function is equivalent to:
def any (iterable):
For element in iterable:
If element:
Return False
Return True
Example:
>>> any ([' A ', ' B ', ' C ', ' d ']) #列表list, the elements are not empty or 0true>>> any ([' A ', ' B ', ', ' d ']) #列表list, There is an empty element true>>> any ([0, ', ', False]) #列表list, the element is all 0, ',falsefalse>>> any (' A ', ' B ', ' C ', ' d ')) #元组tuple, the elements are not empty or 0true>>> any ((' A ', ' B ', ', ' d ')) #元组tuple, there is an empty element true>>> any (0, ', false)) #元组tuple, the element is all 0, "', Falsefalse >>> any ([]) # empty list false>>> any (()) # Empty tuple false
All (iterable)
If all elements of iterable are not 0, ', false, or iterable are empty, all (iterable) returns True, otherwise false; The function is equivalent to:
defall(iterable):
for
element
in
iterable:
if
not
element:
return
False
return
TrueExample:
>>> all ([' A ', ' B ', ' C ', ' d ']) #列表list, the elements are not empty or 0true>>> all ([' A ', ' B ', ', ' d ']) #列表list, There is an empty element false>>> all ([0, 3]) #列表list, there is an element of 0 false >>> all ((' A ', ' B ', ' C ', ' d ')) #元组tuple, the elements are not empty or 0true>>> all ((' A ', ' B ', ', ' d ') #元组tuple, there is an empty element false>>> all (0, 1, 2, 3)) #元组tuple, there is an element of 0 false >>> all ([]) # Empty list true>>> All (()) # Empty tuple True
Note: Empty tuple, empty list return value is true, here is to pay special attention to
Python function any () and all ()