python heapq和accumulate

Mainly for the purpose of brushing the questions, there are always built-in python libraries such as heap and prefix sum, so make a record

1 heapq

  The heapq library is one of the Python standard libraries. It provides methods for building small top heaps and some basic operations on small top heaps (such as entering and exiting the heap, etc.), which can be used to implement heap sorting algorithms. A heap is a data structure, a complete binary tree, which satisfies the property: the value of each node is greater than or equal to (less than or equal to) the value of its left and right child nodes. The small top heap is used in python heapq.
   In the heapq library, the data type used by heapq is the basic data type list of python. To satisfy the nature of the heap, the value of the index k must be <= 2k+1 and 2k+2.

1.1 heapq creates a heap

# coding=utf-8
import heapq
 
array = [10, 17, 50, 7, 30, 24, 27, 45, 15, 5, 36, 21]
heap = []
for num in array:
    heapq.heappush(heap, num)
print("array:", array)
print("heap: ", heap)
 
heapq.heapify(array)
print("array:", array)

# 打印的结果
'''
array: [10, 17, 50, 7, 30, 24, 27, 45, 15, 5, 36, 21]
heap:  [5, 7, 21, 15, 10, 24, 27, 45, 17, 30, 36, 50]
array: [5, 7, 21, 10, 17, 24, 27, 45, 15, 30, 36, 50]
'''

There are two ways to create a heap in heapq:

  • heappush(heap, num): Create an empty heap first, and then add data to the heap one by one
  • heapify(array): Directly receive the list array and adjust the list array to a small top heap

The results achieved by the two methods will be different. For example, in the above code, the heap structure obtained by using heappush(heap, num) is as follows.

insert image description here
The heap structure obtained by using heapify(array) is as follows.
insert image description here
However, both of these results satisfy the characteristics of the small top heap and do not affect the use of the heap (the heap will only fetch data from the top of the heap, and the structure will be readjusted after the data is fetched).

1.2 heapq implements heap sorting

array = [10, 17, 50, 7, 30, 24, 27, 45, 15, 5, 36, 21]
heap = []
for num in array:
    heapq.heappush(heap, num)
print(heap[0])
# print(heapq.heappop(heap))
heap_sort = [heapq.heappop(heap) for _ in range(len(heap))]
print("heap sort result: ", heap_sort)

# 利用heappop可以出堆操作,剩下的数据重新构造成新的小顶堆
# 打印的结果
'''
5
heap sort result:  [5, 7, 10, 15, 17, 21, 24, 27, 30, 36, 45, 50]
'''

1.3 Get the maximum or minimum value in the heap

array = [10, 17, 50, 7, 30, 24, 27, 45, 15, 5, 36, 21]
heapq.heapify(array)
print(heapq.nlargest(2, array)) # 从小顶堆中选出最大的两个数
print(heapq.nsmallest(3, array))

# 打印的结果
'''
[50, 45]
[5, 7, 10]
'''

1.4 Merge multiple heaps

li=[1, 1, 4, 2, 3, 5, 8]
li1=[2,1,2,3,2,4,6]
heapq.heapify(li)
heapq.heapify(li1)
lists=heapq.merge(li,li1)
print(list(lists))

'''
打印的结果
[1, 1, 2, 1, 2, 3, 2, 4, 2, 3, 4, 5, 6, 8]
'''

2 accumulate

accumulate (iterable object), the function is to perform an operation on the incoming iter object one by one (the default is to accumulate), see the following code for details

>>> a=[1,2,3,4,5]
>>> b=accumulate(a)  #默认是累加
>>> b   #这里返回的是一个可迭代对象
<itertools.accumulate object at 0x7f3e5c2f4e48>
>>> list(b)   #强制转化
[1, 3, 6, 10, 15]
>>> 

Pass in the operation you want, such as replacing it with multiplication

>>> import operator
>>> c=accumulate(a,operator.mul)
>>> c
<itertools.accumulate object at 0x7f3e5c2f4f88>
>>> list(c)
[1, 2, 6, 24, 120]
>>> 

Guess you like

Origin blog.csdn.net/weixin_45074568/article/details/124902510