【发布时间】:2022-01-27 15:52:01
【问题描述】:
我觉得我一定是误解了 heappushpop,因为我无法弄清楚为什么会发生这种情况。
sequence = [2, 6, 4, 5, 8]
result = []
x = [3, -1]
result.append(heapq.heappushpop(x, sequence[0]))
这不应该将 2 压入堆 x 并弹出 -1 吗?我正在推动 2 并弹出。
【问题讨论】:
-
x不是堆并且顺序不正确,您需要先调用heapq.heapify(x),然后才能期望x正确地作为堆运行