Easy to forget Python library

Easy to forget Python library

Other libraries, such as insect, deque and queue can refer to: https://blog.csdn.net/a40850273/article/details/95356001 

cmp_to_key function under functools

Python 3 has cancelled the cmp parameter of the built-in functions sort and sorted functions, so Python 3 sorting can only accept functions with a single input variable as the key parameter. However, a function with two input parameters can be passed in as the key parameter through cmp_to_key under functools. A simple example is given below.

from functools import cmp_to_key 
L=[9,2,23,1,2]
 
sorted(L,key=cmp_to_key(lambda x,y:y-x))
输出:
[23, 9, 2, 2, 1]
 
 
sorted(L,key=cmp_to_key(lambda x,y:x-y))
输出:
[1, 2, 2, 9, 23]

It should be noted that the function passed in cmp_to_key needs to return positive, negative and zero to express the relationship between the two numbers. If it is understood that two parameters are passed in in order, then returning a positive number means that the order of the two numbers needs to be changed, and vice versa.

heapq heap

Heapq implements the classic heap and provides the following methods:

  • heapq.heappush(heap, item) adds item to heap (heap is a list)
  • heapq.heappop(heap) pops the top element of the heap, and returns the top of the heap
  • heapq.heappushpop(heap, item) first add the item to the heap, then pop it, much faster than heappush() and heapop()
  • heapq.heapreplace(heap, item) pop first, and then add item to the heap, which is much faster than heapp() and then heappush()
  • heapq.heapify(x) Heap adjustment of list x, the default is small top heap
  • heapq.merge(*iterables) merges multiple lists and adjusts the heap, and returns an iterator of the merged list
  • heapq.nlargest(n, iterable, key=None) returns the largest n elements (Top-K problem)
  • heapq.nsmallest(n, iterable, key=None) returns the smallest n elements (Top-K problem)
import heapq
import random
 
# Top-K
mylist = list(random.sample(range(100), 10))
k = 3
largest = heapq.nlargest(k, mylist)
smallest = heapq.nsmallest(k, mylist)
print('original list is', mylist)
print('largest-'+str(k), '  is ', largest)
print('smallest-'+str(k), ' is ', smallest)
 
# heapify
print('original list is', mylist)
heapq.heapify(mylist)
print('heapify  list is', mylist)
 
# heappush & heappop
heapq.heappush(mylist, 105)
print('pushed heap is', mylist)
heapq.heappop(mylist)
print('popped heap is', mylist)
 
# heappushpop & heapreplace
heapq.heappushpop(mylist, 130)    # heappush -> heappop
print('heappushpop', mylist)
heapq.heapreplace(mylist, 2)    # heappop -> heappush
print('heapreplace', mylist)
'''
original list is [99, 44, 51, 45, 24, 82, 70, 97, 83, 3]
largest-3   is  [99, 97, 83]
smallest-3  is  [3, 24, 44]
original list is [99, 44, 51, 45, 24, 82, 70, 97, 83, 3]
heapify  list is [3, 24, 51, 45, 44, 82, 70, 97, 83, 99]
pushed heap is [3, 24, 51, 45, 44, 82, 70, 97, 83, 99, 105]
popped heap is [24, 44, 51, 45, 99, 82, 70, 97, 83, 105]
heappushpop [44, 45, 51, 83, 99, 82, 70, 97, 130, 105]
heapreplace [2, 45, 51, 83, 99, 82, 70, 97, 130, 105]
'''

 

 

Guess you like

Origin blog.csdn.net/a40850273/article/details/104271980