In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to use the re.findall () function in the python regular expression module". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor learn how to use the re.findall () function in the python regular expression module.
First, import the python regular expression module "re":
Import re
Suppose you have the following string:
Test_string1= 'Python is Amazon'
The expression r "^\ w +" can be passed to "re.findall" along with the string, which returns the beginning of the input string:
Reregex_1 = re.findall (r "^\ w +", test_string1) print (regex_1)
In the expression r "^\ w +", the character "^" corresponds to the beginning of the string, while "\ w +" looks for alphanumeric characters in the string.
If you remove the "^", you will get:
Reregex_1 = re.findall (r "\ w +", test_string1) print (regex_1)
Try to extract the beginning of another string example:
Test_string2= 'Java is Amazon'
Now, apply "re.findall ()" to find the first word of the string:
Reregex_2 = re.findall (r "^\ w +", test_string2) print (regex_2)
Next, consider a more practical scenario. Suppose you have a list of YouTube video titles and the corresponding number of YouTube views. We may be interested in analyzing the relationship between the first word of the video title and the corresponding number of video views. Consider the following title / viewing tuple list:
Youtube_titles= [("How to Tell if We're Beating COVID-19", 2200000), ("ExtremeCloset Clean Out", 326000), ("This is $1000000 inFood", 8800000), ("How To Tell If Someone Truly Loves You", 2800000), ("How to Tell Real Gold from Fake", 2300000), ("Extreme living room transformation", 25000)]
You can find the first word of each title in the following ways:
For titlein youtube_titles: print (re.findall (r "^\ w +", title [0]) [0])
You can add these values to the list:
First_words= [] for title in youtube_titles: first_words.append (re.findall (r "^\ w +", title [0]) [0]) print (first_words)
You can also append the number of views to the list:
First_words= [] views = [] for title in youtube_titles: first_words.append (re.findall (r "^\ w +", title [0]) [0]) views.append (title [1])
You can then create a data box for the video header value and the number of times the video is viewed:
Importpandas as pd df = pd.DataFrame ({'first_words': first_words,' views':views}) print (df)
You can then group the headwords of each title and calculate the average number of views of each headline:
Dfdf = df.groupby ('first_words') [' views'] .mean () print (df)
Sort these values in descending order:
Dfdf = df.groupby ('first_words') [' views'] .mean () .sort_values (ascending = False) print (df)
Assuming that these results come from a large enough dataset (for example, thousands of titles and views), this type of analysis can help us choose the best YouTube video title.
At this point, I believe you have a deeper understanding of "how to use the re.findall () function in the python regular expression module". 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.