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 個元素。

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