Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我对堆弹出输出感到困惑。
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]
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]。第一个元素用于对最小堆进行排序。