我在Python中使用了heapq模块,发现无论我是否使用reverse=True,我只能获得最小堆。
即使我使用了reverse=True,我仍然得到了最小的顶部堆。
from heapq import *
h=[]
merge(h,key=lambda e:e[0],reverse=True)
heappush(h, (200, 1))
heappush(h, (300,2))
heappush(h, (400,3))
print(heappop(h))
我仍然得到了结果:
(200, 1)
我想要获得结果:
(400,3)
如何做?
哪个是最小的元素。我想弹出最大的元素?
附注:这是问题的一部分,找到最大值,然后将其拆分为几个元素,然后将其放回堆中。
nlargest(h, 1)
? - cs95max(h)
的方式。 - Stefan Pochmann