当前位置:网站首页>Answer fans' questions | count the number and frequency of letters in the text

Answer fans' questions | count the number and frequency of letters in the text

2022-06-11 21:21:00 gongsuochen


WeChat official account :" Beauty of algorithm and programming ", Welcome to your attention , Learn more about this series article

1 Preface

In recent days, , Xiaobian received a message from a fan , Want to know how to use python Count the number and frequency of English letters in the text . So close up this article article To answer questions for this fan .

 Answer questions from fans | Count the number and frequency of letters in the text _ character string

chart 1 Fans of the message

2 Problem solving

This problem can be divided into two parts , One is to use python Realize the operation of text file , Two is python Count the string . For your convenience , Xiaobian will talk about the second part first and then the first part .

2.1 use python Count the number and frequency of English letters in a given string .

There are many ways to count the number of occurrences of English letters in a string , Xiaobian will introduce you to a relatively simple , Easy to understand .

“ Statistics ”. To count the number of occurrences of a character , stay python Can be used directly in count() function , For example, the characters to be counted are i, String is text_file, See code for its usage .

count = text_file.count(i) # Count the letters in original text Number of occurrences in this

Through this line of code , You can count the number of occurrences of the desired character in the string , Then the frequency of its occurrence can be directly divided by the number of occurrences divided by the total number of characters . The next step is to count multiple English letters .

“ Filter ”. To count multiple letters , You need to keep performing the first step , So we need to use for loop ,i Just traverse... In the target string . Because you only need to count the English letters , So you can also use python Its own judgment function isalpha(). In addition, in order not to repeat the statistics of the characters during traversal , So you need to do a process on the target string , use python Of set() Function can convert a string into a collection ( Automatically filter duplicate characters ). See the code for details. .


text_file_set = sorted(set(text_file)) # Sort the text and return the set without duplicate characters

for i in text_file_set: # Traversal of the resulting text collection

    if i.isalpha(): # Determine whether it is an English letter

        count = text_file.count(i) # Count the letters in original text Number of occurrences in this

        frequency = count/text_count # Calculate the frequency of the letter

        print("%s:%d;%.4f" % (i,count,frequency)) # Output what you want ( Keep the frequency to four decimal places )


2.2python Realize the operation of text file .

Go through the above steps , The letter statistics of the given string has been realized , The given way is to input , Of course, it's troublesome to input by yourself , Even paste and copy is troublesome , So we need to use python Built in functions for open() To help open the target text file . Here's a brief introduction to the following open() Function usage .

open(‘file.name’,’mode’,......)

There are many parameters after the quotation marks , I won't go into details here , Just remember the common ones mode Type and function .

w: Open... In write mode

a: Open... In append mode

r: Open in read-only mode

r+: Turn on in read-write mode

w+: Turn on in read-write mode

rb: Open in binary read mode

wb: Open in binary write mode

ab: Open in binary append mode

rb+: Open in binary read-write mode

wb+: Open in binary read-write mode

ab+: Open in binary append mode

Then use this line of code to open the target text file .

text = open('test.txt','r') # Open target text

Be careful : there test.txt Is under the same path as the code , If not , You need to enter the full path name .

After opening the file , It can't be used directly , Because its type is not str, So you need to add another line of code to text Convert to an operable string type (str).

text_file = text.read() # Read target text ( Convert to string type )

And then you can use type Function to see if str type .


print(type(text),type(text_file))

<class '_io.TextIOWrapper'> <class 'str'>


It can be found that the text has been changed from the original txt Type to str type . Then you can put the code together , Then the problem is solved .

3 Complete code


text = open('test.txt','r') # Open target text

text_file = text.read() # Read target text ( Convert to string type )

text_count = len(text_file) # Returns the total number of characters in the text

text_file_set = sorted(set(text_file)) # Sort the text and return the set without duplicate characters

for i in text_file_set: # Traversal of the resulting text collection

    if i.isalpha(): # Determine whether it is an English letter

        count = text_file.count(i) # Count the letters in original text Number of occurrences in this

        frequency = count/text_count # Calculate the frequency of the letter

        print("%s:%d;%.4f" % (i,count,frequency)) # Output what you want ( Keep the frequency to four decimal places )


Let's see some running effects :

 Answer questions from fans | Count the number and frequency of letters in the text _python_02

chart 2 original text Contents of this

 Answer questions from fans | Count the number and frequency of letters in the text _python_03

chart 3 Run... Part of the effect

4 summary

The problem itself is not difficult , The code is also simple , The small is written in such detail for everyone to understand , I hope that no matter what problems you encounter in the future, you can solve them in this way of thinking step by step .

After the above explanation , I wonder if this fan understands ? If not, please leave a message below . And if you have any problems that you can't solve or don't understand , You can leave a message in the official account , All the editors of official account will try their best to answer your questions .

END

Lord   Ed    |    Wang Wenxing

responsibility   Ed    |    Jianglaihong

 where2go The team



WeChat Number : Beauty of algorithm and programming           

 Answer questions from fans | Count the number and frequency of letters in the text _ character string _04

Long press identification QR code to follow us !

“ Write a message ” Comment , Looking forward to your participation !



原网站

版权声明
本文为[gongsuochen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203011700372862.html