本文共 487 字,大约阅读时间需要 1 分钟。
In [1]: import heapq
In [2]: h = []
In [3]: h.append(1)
In [4]: h.append(2)
In [5]: h.append(3)
In [6]: h.append(8)
In [7]: h.append(5)
In [8]: h.append(0)
#用heapq 生成一个最小堆In [9]: heapq.heapify(h)In [10]: heapq.heappop()
In [11]: heapq.heappop(h)
Out[11]: 0In [12]: heapq.heappop(h)
Out[12]: 1In [13]: heapq.heappop(h)
Out[13]: 2In [14]: heapq.heappop(h)
Out[14]: 3In [15]: heapq.heappop(h)
Out[15]: 5In [16]: heapq.heappop(h)
Out[16]: 8转载于:https://blog.51cto.com/12080420/2389385