Python heapq模組,pythonheapq模組

來源:互聯網
上載者:User

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]

希望能對你有所協助!




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.