-2

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

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]

4

1 回答 1

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]。第一个元素用于对最小堆进行排序。

于 2019-06-23T17:31:26.420 回答