Python heapq模組,pythonheapq模組
這個模組(build-in)實現了一個堆的資料結構,完美的解決了Top-K問題,以後解決Top-K問題的時候,直接把這個模組拿來用就可以了
注意,預設的heap是一個小頂堆!
heapq模組提供了如下幾個函數:
heapq.heappush(heap, item)把item添加到heap中(heap是一個列表)
heapq.heappop(heap) 把堆頂元素彈出,返回的就是堆頂
heapq.heappushpop(heap, item) 先把item加入到堆中,然後再pop,比heappush()再heappop()要快得多
heapq.heapreplace(heap, item) 先pop,然後再把item加入到堆中,比heappop()再heappush()要快得多
heapq.heapify(x) 將列表x進行堆調整,預設的是小頂堆
heapq.merge(*iterables) 將多個列表合并,並進行堆調整,返回的是合并後的列表的迭代器
heapq.nlargest(n, iterable, key=None) 返回最大的n個元素(Top-K問題)
heapq.nsmallest(n, iterable, key=None) 返回最小的n個元素(Top-K問題)
下面看下樣本:
import heapqimport random# Top-Kmylist = list(random.sample(range(100), 10))k = 3largest = 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)# heapifyprint('original list is', mylist)heapq.heapify(mylist)print('heapify list is', mylist)# heappush & heappopheapq.heappush(mylist, 105)print('pushed heap is', mylist)heapq.heappop(mylist)print('popped heap is', mylist)# heappushpop & heapreplaceheapq.heappushpop(mylist, 130) # heappush -> heappopprint('heappushpop', mylist)heapq.heapreplace(mylist, 2) # heappop -> heappushprint('heapreplace', mylist)
輸出結果為:
original list is [18, 6, 10, 24, 48, 2, 9, 25, 16, 89]largest-3 is [89, 48, 25]smallest-3 is [2, 6, 9]original list is [18, 6, 10, 24, 48, 2, 9, 25, 16, 89]heapify list is [2, 6, 9, 16, 48, 10, 18, 25, 24, 89]pushed heap is [2, 6, 9, 16, 48, 10, 18, 25, 24, 89, 105]popped heap is [6, 16, 9, 24, 48, 10, 18, 25, 105, 89]heappushpop [9, 16, 10, 24, 48, 130, 18, 25, 105, 89]heapreplace [2, 16, 10, 24, 48, 130, 18, 25, 105, 89]
希望能對你有所協助!