什么堆弹出返回?

时间:2019-06-23 17:26:26

标签: python heapq

我对堆弹出输出感到困惑。

import heapq
heapq.heappush(PQ, ['b', 0.95])
heapq.heappush(PQ, ['d', 0.72])
heapq.heappush(PQ, ['e', 1.75])

a = heapq.heappop(PQ)
print (a)

它返回:['b',0.95] 为什么不返回['d',0.72]

1 个答案:

答案 0 :(得分:2)

import heapq
PQ = []
heapq.heappush(PQ, ['z', 0.95]) # Changed it to z which is bigger 
heapq.heappush(PQ, ['d', 0.72])
heapq.heappush(PQ, ['e', 1.75])

a = heapq.heappop(PQ)
print (a)

结果为['d',0.72]。第一个元素用于对最小堆进行排序。