Home > Article > Backend Development > A brief introduction to the use of any() and all() in Python
This article brings you a brief introduction to the use of any() and all() in Python. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Introduction
In ordinary text processing work, I often encounter such a situation: use python to determine whether a string contains a elements in the list.
It will be very concise to use python’s built-in function any() at this time:
fruits = ['apple', 'orange', 'peach'] str = "I want some apples" if any(element in str for element in fruits): print "string contains some fruits."
any()
In fact The any function is very simple: determine whether a tuple or list is all empty, 0, False. If it is all empty, 0, False, it returns False; if it is not all empty, it returns True.
all()
all function is just the opposite of any: determine whether a tuple or list is complete Is not empty, 0, False. If none are empty, returns True; otherwise returns False.
It should be noted here that the return value of empty tuple and empty list is True
Related recommendations:
python Usage analysis of map, any, and all functions
A brief introduction to the use of the readline() method in Python
The above is the detailed content of A brief introduction to the use of any() and all() in Python. For more information, please follow other related articles on the PHP Chinese website!