In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use the Python any () and all () functions". Interested friends might as well take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor learn how to use the Python any () and all () functions.
Boolean data types in Python
Before we go into any () and all (), let's take a quick look at the Boolean data types in Python. You can call the bool () method to get the true value of any Python object. You can run the following code example in your favorite IDE.
# truth value of None is Falseprint (bool (None)) # OutputFalse# truth value of an empty string (") is Falseprint (bool (")) # OutputFalse# truth value of an empty list (or any iterable) is Falseprint (bool ([])) # OutputFalse# truth value of 0 {int (0), float (0.0) and complex (0j)} is Falseprint (bool (0)) # OutputFalse
As shown in the above clip
None has a true False.
Numeric zero (0)-integers, floating-point numbers, and complex numbers represent 0 and all have a true value False
The true value of all empty iterable objects, such as lists, tuples, and strings, is False.
In other words, the true values of all non-zero and non-empty iterable objects are fairly intuitive True.
How to use the any () function in Python
Let's look at the syntax of the any () function, look at some simple examples, and then look at more useful examples.
Syntax: any (iterable)
Returns True and True as long as at least one item in the iterable object is.
Returns False if the iterable object is empty.
Therefore, the any () function takes an iterable object as a parameter, and True returns True as long as at least one item in the iterable object is.
Here are a few simple examples to verify how the any () function works:
List_1 = # any (a list with at least one non-zero entry) returns Trueprint (any (list_1)) # OutputTruelist_2 = # any (a list of zeros) returns Falseprint (any (list_2)) # OutputFalselist_3 = [True, False, False] # any (a list with at least one True value) returns Trueprint (any (list_3)) # OutputTruelist_4 = [ "code more"] # any (a list with at least one non-empty string) returns Trueprint (any (list_4)) # OutputTruelist_5 = [",", "] # any (a list of empty strings) returns Falseprint (any (list_5)) # OutputFalse how to use Python's any () function to check for numbers in a string
Now let's use the any () function to check if there are any numbers in the string. Let's write down the steps.
Check: are there any numbers in the string?
Traverses the string to access each character in the string.
Check whether each character is a number by calling its method isdigit ().
Isdigit () returns True if the character being tested is a number, False otherwise.
List derivation is very helpful for collecting all these truth values in a list. This is a quick review:
# List Comprehension [output_expression for every_item in an_iterable] | | V result of doing something on each item in the iterable # In essence, Loop through the iterable, do something on each item and return the result of the operation.
As shown in the following code snippet, our sample string coding**is**cool**345 contains numbers.
Therefore, when any () calls a function on a string, it should return True. We use list understanding to get a list of True and false values based on whether the character is a number or not.
My_string = "coding**is**cool**345" are_there_digits = [char.isdigit () for char in my_string] print (any (are_there_digits)) # OutputTrue
Notice that the number of items in the are_there_digits list is as many as the length of the string.
For each character in the string, there is a corresponding true value-- if the character is a number corresponding to True, if the character is not a number corresponding to False, as shown below.
Print (are_there_digits) # Output [False,False, True, True, True] how to check letters in a string using Python's any () function
Let's give another similar example. This time, let's check for the presence of letters in the string.
The string being tested (* 456278)) does not contain letters-- False is returned by calling any () as expected. For each character in the string, call the isalpha () method to check whether it is a letter.
My_string = "* 456278))" num = [char.isalpha () for char in my_string] print (any (num)) # OutputFalse
This is_letter is a list of False values, as follows:
Print (is_letter) # Output [False, False] how to use Python's any () function to combine multiple conditions with logic or
Suppose you decide to be more productive and write down the list shown below. However, you choose not to be too hard on yourself and decide that as long as one of the items on the list happens, you can have a lot of candy! [
Notice how we consider multiple conditions, but even if one of them is evaluated as, candy True will be selected.
Is this very similar to the statement that if you need to check that multiple conditions linked by the logical or operator evaluate to True? Yes, it is, and the any () function can come in handy when doing so.
Suppose you have conditional C1, c2, c3,..., cN. Consider the following pseudo code:
If c1 or c2 or... C _ (NMur1) or CN: # DO THISelse: # DO THIS
You can now collect all of these conditions in an iterable object, such as a list or tuple, and then call any () the iterable object to check whether one or more conditions are True, as shown below. Isn't it easy?
Conditions = [C1 DO THISelse c2me., cymn] if any (conditions): # DO THISelse: # DO THIS how to use the all () function in Python
Let's start with the syntax of the all () function.
Syntax: all (iterable)
Returns True if bool (x) is an iterator of all values x of True.
True returns if the iterable object is empty.
The all () function takes an iterable object as a parameter, and True returns only if all items in the iterable object evaluate to True or if the iterable object is empty. In all other cases, the all () function returns False.
How to use the all () function of Python to check letters in a string
Let's examine some of the characteristics of a string with a similar example.
In addition to letters, the test string coding**is**cool contains the special character *. Therefore, when we use the all () function to check whether all characters in the string are letters, we should get False.
My_string = "coding**is**cool" are_all_letters = [char.isalpha () for char in my_string] print (all (are_all_letters)) # OutputFalseprint (are_all_letters) # Output [True, False, False,True, True, False, False,True, True]
Notice how the list has a False value * at all locations in the string where the are_all_letters appears.
How to use the all () function of Python to check numbers in a string
Now let's use the all () function to check that all the characters in the string are numbers. The test string 56456278 contains only numbers, so the call to all () should return, True because the list derivation provides us with a list of truth values.
My_string = "56456278" are_all_digits = [char.isdigit () for char in my_string] print (all (are_all_digits)) # OutputTrueprint (are_all_digits) # Output [True, True] how to use the all () function of Python to combine multiple conditions with logical AND
Let's consider the following example. This time, you are competing for iPad, and the conditions are more stringent. You must complete all the tasks in the list to get iPad from your cousin.
Now, this is similar to using the if statement to check whether multiple conditions linked by the logical and operator are evaluated as very similar to True, as follows:
If c1 and c2 and... C _ (NMur1) and CN: # DO THISelse: # DO THIS
You can use the all () function to make all this more concise by collecting the conditions in the iterable and then calling the all () function in the iterable.
Conditions = [c1DO THISelse c2recover.cendN] if all (conditions): # DO THISelse: # DO THIS so far, I believe you have a better understanding of "how to use the Python any () and all () functions". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.