This time I will show you how to count the number of occurrences of letters in Python. What are the precautions for counting the number of occurrences of letters in Python? The following is a practical case, let's take a look.
Title:
Count the number of occurrences of each word in a file and list the 5 most frequently occurring words.Foreword:
This question is widely used in practical application scenarios, such as the statistics of high-level students who have appeared in the CET-4 and CET-6 exams over the years. Frequency vocabulary, I remember that Li Xiaolai used his programming skills to publish a best-selling book on word memorization. He memorized words based on word frequency, which was very popular among students. This is a typical scenario where programming skills are used to solve real problems. In addition, during data analysis, those word cloud effects are essentially based on word frequency statistics to adjust the font size. If you can skillfully use the knowledge in Python to solve problems, it means that you are really getting started with Python.Analysis
This question mainly examines the following knowledge points:1. How to read correctly Write files
To read and write files in python, you can use theAnother point is that the file descriptor needs to be closed after reading and writing the file. In addition to using the try...except...finally syntax, we can also use the more elegant with... as syntax. to automatically close the file.2. How to sort data
The sorted function is a frequently used built-in function, and its usage is also very powerful because it can specify parameters key to perform custom sorting, which means that you can not only sort numbers, letters, but also lists, dictionaries, and custom objects. You only need to tell the sorted function what the sorting rules are, such as For a people object, I can sort it by age or height and weight, so this function is very flexible. In addition, there is a sort method for list objects. If you can clearly distinguish the difference between list.sort and sorted That means you can already use it flexibly.3. The use of dictionary data type
To do word frequency statistics, using a dictionary is undoubtedly the most appropriate data type, with words as the keys of the dictionary , the number of times a word appears is used as the value of the dictionary, and the frequency of each word is easily recorded. The dictionary is much like our phone book, with each name associated with a phone number. In addition, the biggest feature of the dictionary is that its query speed is very fast. Under ideal circumstances, the time complexity is O(1). I mean ideal circumstances. If you want to learn more about dictionaries, it is recommended to read this articlehttps://www.laurentluce.com/posts/ python-dictionary-implementation/
For text and string processing, regular expressions are simply an artifact. , it is very widely used whether it is doing data crawling or data cleaning. Of course, regular expressions are not unique to Python. They are supported by allImplementation
After the analysis, we can actually implement it very quickly. So when we get a requirement, we must first clarify the requirement and think about what technologies can be used to achieve it, and then start writing code. In fact, at work, we actually spend less than half of the time writing code. .# -*- coding:utf-8 -*- import io import re class Counter: def init(self, path): """ :param path: 文件路径 """ self.mapping = dict() with io.open(path, encoding="utf-8") as f: data = f.read() words = [s.lower() for s in re.findall("\w+", data)] for word in words: self.mapping[word] = self.mapping.get(word, 0) + 1 def most_common(self, n): assert n > 0, "n should be large than 0" return sorted(self.mapping.items(), key=lambda item: item[1], reverse=True)[:n] if name == 'main': most_common_5 = Counter("importthis.txt").most_common(5) for item in most_common_5: print(item)Print results:
('is', 10)
('better', 8)
('than', 8)
('the', 6)
('to' , 5)
Summary
When I look at your code, many codes still have irregular naming (recommended to read PEP8), the code layout is confusing (it is difficult to read, it is recommended to use Pycharm for formatting). There are also many codes whose implementation methods look very complicated (the more complex the code, the more bugs it usually has). Of course, the implementation method is not the only one.
For example, the Python module itself provides a collections.Counter class, which inherits from the dict class and is used for statistics. I found that some students use this class to implement it. Please be careful. You may have discovered that the Counter I implemented is very similar to the Counter under collections. In fact, this is making wheels. Making wheels can exercise our programming thinking. Of course, if you have ready-made things at work, there is no need to make your own wheels, unless you Have the confidence to do better. You can also think about what you would do if Python did not provide the Counter tool.
In addition, this module also provides an ordered dictionary object OrderedDict, which can save us from manual sorting operations. Finally, I recommend that you study and summarize all the content I mentioned above. If you can persist for 100 days, I believe you will have a good grasp of Python.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
How to convert a matrix into a list in Python
##Lists, arrays, and matrices are mutually exclusive in python Conversion method
The above is the detailed content of How to count the number of occurrences of letters in Python. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),