当前位置:网站首页>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 !
边栏推荐
- Website online customer service system Gofly source code development log - 2 Develop command line applications
- UML系列文章(29)体系结构建模---模式和框架
- 常用文件函数
- JVM object allocation policy TLAB
- Stream Chinese sorting
- Application business layer modification
- php pcntl_ Fork create multiple child process resolution
- PHP strtotime 获取自然月误差问题解决方案
- Go语言for循环
- JS monitor scrolling touch bottom load more_ Browser scrolls to the bottom to load more
猜你喜欢

【博弈论-完全信息静态博弈】 战略式博弈
![[data visualization] use Apache superset to visualize Clickhouse data](/img/4b/a73c2eb810f1d2b492e950afb2d0bc.png)
[data visualization] use Apache superset to visualize Clickhouse data

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

Cs144 lab0 lab1 record

My collection of scientific research websites

Application scenario: wide application of Poe network card in NDI technology for live broadcast program production

Iros 2021 | new idea of laser vision fusion? Lidar intensity diagram +vpr

解决 img 5px 间距的问题

Deriving Kalman filter from probability theory

Weekly 02 | to tell you the truth, I am actually a student of MIT
随机推荐
In idea, run the yarn command to show that the file cannot be loaded because running scripts is disabled on this system
Frequency domain filter
Go language for loop
One article to show you how to understand the harmonyos application on the shelves
第一部分 物理层
[game theory complete information static game] strategic game
正则校验匹配[0-100]、[0-1000]之间的正整数或小数点位数限制
[Game Theory - introduction]
select _ Lazy loading
Compilation process of program
ORA-04098: trigger ‘xxx.xxx‘ is invalid and failed re-validation
Which Bluetooth headset is better within 500? Inventory of gifts for girls' Day
为什么需要微服务
[nk] 牛客练习赛100 C 小红的删数字
Application business layer modification
JVM之对象创建过程
Docker installation redis
RANSAC提取圆柱(MATLAB内置函数)
周刊02|不瞞你說,我其實是MIT的學生
可综合RTL代码设计方法和注意事项