当前位置:网站首页>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 .

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 :

chart 2 original text Contents of this

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

Long press identification QR code to follow us !
“ Write a message ” Comment , Looking forward to your participation !
边栏推荐
- Application scenario: wide application of Poe network card in NDI technology for live broadcast program production
- Release of version 5.6 of rainbow, add multiple installation methods, and optimize the topology operation experience
- Pyqt5 technical part - set the default value of qcombobox drop-down box and get the current selection of the drop-down box
- Teach you how to use win7 system to quickly build your own website
- 常用文件函数
- 应用业务层修改
- BUG -- coredump使用
- Lr-link Lianrui makes its debut at the digital Expo with new products - helping the construction of new infrastructure data center
- 【博弈论-完全信息静态博弈】 战略式博弈
- Flutter implements the JD address selection component
猜你喜欢

What are striplines and microstrip lines? Reference planes and transmission lines

Teach you how to use win7 system to quickly build your own website

JVM object allocation policy TLAB

Obsidian关系图谱如何让节点可以手动拖动

In idea, run the yarn command to show that the file cannot be loaded because running scripts is disabled on this system

php pcntl_ Fork create multiple child process resolution

Online excel file parsing and conversion to JSON format

【博弈论-完全信息静态博弈】 战略式博弈

gateway先启动其他微服务,在启动网关,网关启动不了,且无异常日志;先启动网关,所有服务能正常启动

Part II data link layer
随机推荐
Explanation of each column output by explain statement
Regular check matches positive integer or decimal limit between [0-100] and [0-1000]
全球机器视觉市场规模持续上涨,PoE图像采集卡为工业相机提供高速传输通道
Serval and Rooted Tree(CF1153D)-DP
Technical exchange | why should network security equipment use bypass function
[data visualization] Apache superset 1.2.0 tutorial (III) - detailed explanation of chart functions
Only 38 years old! Zhou Chuan, a researcher of the Chinese Academy of Sciences, died unfortunately. Rao Yi sent a document to mourn: he guided me when he was still my student
JVM运行时常量池以及直接内存
为什么100G网络传输要使用iWARP、RoCE v2、NVMe-oF等协议
stream中文排序
New product release: lr-link Lianrui launched the first 25g OCP 3.0 network card
ASCII码对照表
Online excel file parsing and conversion to JSON format
[Game Theory - introduction]
JS monitor scrolling touch bottom load more_ Browser scrolls to the bottom to load more
Obsidian关系图谱如何让节点可以手动拖动
Why microservices are needed
Flutter implements the JD address selection component
How to Load Data from CSV (Data Preparation Part)
Syntax of SQL