In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "what are the books that must be read by Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Python cookbook is a classic, written by David Beazley, who has more than 20 years of experience in using Python, coupled with his strong writing skills, so it's worth reading.
There are also many translated versions of it, among which the more excellent version is translated by yidao620c. Today, I consult the author of the translation and work for Huawei. Of course, the most important thing is that the content of the book is good. I believe many Python enthusiasts like this way of discussing according to a small topic.
If you want to understand the topic of this book, you still need to have a certain foundation of Python, which is not suitable for pure Python rookies. Let's talk about this book, what's good about it and how to use it.
This is its overall outline directory, including a total of 15 chapters, respectively discussed: data structures, strings, numbers, iterators and generators, files and IO, functions, classes, modules, network programming, concurrent and testing. These are the core knowledge modules in Python. When David, the author of the original book, personally recommends solving practical problems, priority should be given to whether the module built in Python can solve the problem.
So what's so good about this book? It is discussed by topic, and each topic is concise enough to have no nonsense. Maximize the summary of each knowledge point from the practical application, such as in the topic: keep the last N elements, the code is beautiful:
From collections import deque
Def search (lines, pattern, history=5):
Previous_lines = deque (maxlen=history)
For line in lines:
If pattern in line:
Yield line, previous_lines
Previous_lines.append (line)
# Example use on a file
If _ _ name__ = ='_ _ main__':
With open (ringing.. Accord cookbook somefile.txt') as f:
For line, prevlines in search (f, 'python', 5):
For pline in prevlines:
Print (pline, end='')
Print (line, end='')
Print ('- * 20)
Keeping a limited history is a time when collections.deque can do its best, and it's rare for the author to make a brief analysis of the above code:
When we write code for query elements, we usually use the generator function that contains the yield expression, as in our example code above. This decouples the search process code from using the search results code
Let's really understand what's good about it, and the built-in modules in Python are really powerful.
For example, when the author is discussing the topic of finding the largest or smallest N elements, on the surface, this is a very simple topic, but in fact, if you want to consider comprehensively, you also need to pay attention to some things. The author discusses:
When the number of elements N = 1, it is recommended to use the max or min method directly.
When the number of lookup elements is close to the length of the entire list, it is recommended that you use the sorted function to get it by slicing.
When the number of elements to find is relatively small, the functions nlargest () and nsmallest () are appropriate.
I believe everyone is familiar with the solutions to the first two cases. The third way to use the built-in module heapq is the heap structure in the algorithm, the common big root heap and small root heap.
> nums = [1, 8, 2, 23, 7,-4, 18, 23, 42, 37, 2]
> import heapq
> heap = list (nums)
> heapq.heapify (heap)
> heap
[- 4, 2, 1, 23, 7, 2, 18, 23, 42, 37, 8]
> > >
After heapify in Python, a small root heap is established by default. Its most important feature is that heap [0] is always the smallest element.
For example, if you want to find the smallest three elements, you can do this by first executing heappop so that the secondary elements become the smallest, as shown in the following figure:
> heapq.heappop (heap)
-4
After executing twice again, you can get that the first three smallest elements of the list are [- 4jue 1pr 2]. At this time, the small root heap is shown in the following figure:
> heapq.heappop (heap)
one
> heapq.heappop (heap)
two
Of course, you can also directly use nsmallest to get the first few minimum values. This is the end of the content of "what are the Python Advanced must-read Books?" Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.