我对堆弹出输出感到困惑。
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]
答案 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]。第一个元素用于对最小堆进行排序。