Python3 堆操作heapq中提供的方法

heapq中常用方法如下:

from heapq import *
heapify(heap)#将heap这个列表转为堆,无需返回值
heappush(heap, item)#将 item加入堆heap。
heappop(heap)#将堆中最小元素弹出。
item=heap[0]#查看堆中最小元素
heapreplace(heap, x)#将堆中最小元素弹出,并将元素x 入堆。
merge(*iterables, key=None, reverse=False)#将多个有序的堆合并成一个大的有序堆,然后再输出。
heappushpop(heap, item)#将item 入堆,然后弹出并返回堆中最小的元素。
nlargest(n, iterable, key=None)#返回堆中最大的 n 个元素。
nsmallest(n, iterable, key=None)#返回堆中最小的 n 个元素。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章