In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to write Python cycle efficiently". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to write Python loops efficiently.
0 preface
When it comes to processing loops, we are used to using for, while, etc., such as printing characters in each list in turn:
Lis = ['love',' python'] for i in lis: print (I) I love python
When the number of bytes of the printed content is small, it will be all loaded into memory and then printed, no problem. However, if there are millions of vehicle tracks, ask you to analyze the travel rules of each customer, traffic jams, and so on, if you are dealing with the matter on a single machine.
You may have to face first, or you may be ignored, and finally, after the code has been written, you may expose a problem: outofmemory, which is often encountered in actual projects.
This problem reminds us that it is very important to write programs that use memory efficiently when dealing with data. Today, let's talk about how to use memory efficiently, save memory and get things done at the same time.
In fact, Python has prepared a module to deal with this, which is the itertools module, in which the functions of several functions are easy to understand.
Instead of giving a general introduction to the functions they can implement, I want to analyze the implementation code behind these functions, how they save memory efficiently, and how Python kernel contributors write beautiful code, which is interesting, isn't it?
OK,let's go. Hope you enjoy the journey!
1 splicing element
The chain function in itertools implements element stitching. The prototype is as follows, and the parameter * represents a variable number of parameters.
Chain (iterables)
The application is as follows:
In [33]: list (chain (['python'], [' very', 'much']) Out [33]: [' python', 'very',' much']
Wow, it doesn't work any better, it's a little bit like join, but it's better than join, and its point is that the parameters are iterable instances.
So, how does chain achieve efficient memory savings? The approximate implementation code of chain is as follows:
Def chain (* iterables): for it in iterables: for element in it: yield element
The above code is not difficult to understand, chain essentially returns a generator, so it actually reads one element at a time into memory, so it saves memory most efficiently.
2 accumulate one by one
Returns the cumulative summary value of the list, prototype:
Accumulate (iterable [, func, *, initial=None])
The application is as follows:
In [36]: list (accumulate ([1 accumulate 2, 3, 4, 5), lambda x, y: X) Out [36]: [1, 2,6,24,120,720]
The approximate implementation code of accumulate is as follows:
Def accumulate (iterable, func=operator.add, *, initial=None): it = iter (iterable) total = initial if initial is None: try: total = next (it) except StopIteration: return yield total for element init: total = func (total, element) yield total
The above code, are you all right? Unlike chain's simple yield, which is a little more complicated here, yield is a bit like return, so the yield total line directly returns an element, the first element of iterable, because the first element returned by this function at any time is its first element. And because yield returns a generator object, such as the name gen, when next (gen), the code will execute to for element in it: this line, and the iterator it has already pointed to the second element of iterable, OK, I believe you understand!
3 funnel screening
It is a compress function, which is similar to the funnel function, so I call it funnel filtering, prototype:
Compress (data, selectors)
In [38]: list (compress ('abcdefg', [1 recorder 1]) Out [38]: [' averse, 'baked,' d']
It is easy to see that the number of elements returned by compress is equal to the shorter list length of the two parameters.
Its approximate implementation code is as follows:
Def compress (data, selectors): return (d for d, s in zip (data, selectors) if s)
This function is very easy to use
4-segment screening
Scan the list and keep it back if the conditions are not met. The prototype is as follows:
Dropwhile (predicate, iterable)
Application examples:
In [39]: list (dropwhile (lambda x: X)
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.