In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
If you get the largest or smallest n elements in a collection, you can use the nlargest and nsmallest functions in the heapq module to meet the requirements.
Heapq introduction:
The heapq module implements heap sorting in python and provides related methods. So that using Python to achieve sorting algorithm has a simple and quick way.
> a = [1, import heapq, 2, 5, 9, 72, and 4]
# get the two smallest elements in the list
> heapq.nsmallest (2) [- 4,1]
# get the two largest elements in the list
> heapq.nlargest (2) [72,9]
Other function descriptions:
Heappush ()
Heapq.heappush (heap, item): press item into the heap array heap. If you do not do this, the subsequent heappop () will fail.
Heappop ()
Heapq.heappop (heap): takes the smallest value from the heap array heap and returns.
> > h = [] # define a list > from heapq import * # introduce heapq module > > h [] > > heappush (hpen5) # increase the value in turn to the heap > heappush (hpen2) > heappush (hpen2) > heappush (hpen9) > h # h value [2,5,3] 9] > heappop (h) # remove the smallest from h And return the value 2 > h [3,5,9] > > h.append (1) # Note: if you append a value to the heap through append instead of pressing it into the heap, the function that appends a value to the heap > h # cannot manipulate the increased value. Or it doesn't exist for the heap [3, 5, 9, 1] > > heappop (h) # the minimum value that can be found from h is 3, not 13 > > heappush (hforce 2) # then not only 2 is pushed into the heap, but 1 is also entered into the heap. > h [1,2,9,5] > heappop (h) # Operand already contains 11
Heapq.heappushpop (heap, item)
It is the combination of heappush and heappop mentioned above, and completes the functions of both at the same time. Note: it is equivalent to operating heappush (heap,item) and then heappop (heap).
> h [1,2,9,5] > heappop (h) 1 > heappushpop (h) 4) # add 4 and delete the minimum value 2 and return the minimum value, which is the same as the following operations: 2 # heappush (hPower4), heappop (h) > h [4,5,9] heapq.heapify (x) x must be list, this function turns list into a heap and operates in real time. Thus it is possible to use heap functions in any case. > heapify (a) # after turning an into a heap, you can operate on it > heappop (a) 1 > > b = [4d2pr 5] # b is not a heap. If you operate on it, the results are shown as follows > heappop (b) # in order Delete the first numeric value and return it without picking the smallest 4 > heapify (b) # after it becomes a heap, then operate > heappop (b) 2
Heapq.heapreplace (heap, item)
Is a joint operation of heappop (heap) and heappush (heap,item). Note that the difference from heappushpop (heap,item) is that the order is different, here it is deleted first and then pressed into the heap
> a = [] > > heapreplace (ap3) # if list is empty Then report error Traceback (most recent call last): File ", line 1, in IndexError: index out of range > heappush (amem3) > > a [3] > > heapreplace (amem2) # first delete (heappop (a)-> 3), then add (heappush (apd2)) 3 > > a [2] > heappush (apreline 5) > heappush (apreline 9) > > heappush (aprit 4) > > a [2,4,9,5] > > heapreplace (a) 6) # first find the minimum value from heap an and return Then add 62 > > a [4, 5, 9, 6] > heapreplace (1) # 1 is added later, and before 1 is added, the minimum value in an is 44 > a [1,5,9,6] heapq.merge (* iterables)
For example:
The use of this function is demonstrated in detail by list (c) [1, 2, 3, 4, 5, 6] in merge sorting. Heapq.nlargest (n, iterable [, key]), heapq.nsmallest (n, iterable [, key]) gets the largest and smallest values in the list. > a [2, 4, 6] > > nlargest (2) [6, 4]
Note: other functions are from https://github.com/qiwsir/algorithm/blob/master/heapq.md
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.