【发布时间】:2018-06-23 15:53:58
【问题描述】:
我在python中使用heapq模块,我发现我只能最小堆,即使我使用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)
怎么做?
这是最小的元素。我要弹出最大的元素?
ps:这是问题的一部分,找到最大值,然后分成几个元素,然后将其放回堆中。
【问题讨论】:
-
nlargest(h, 1)? -
@cᴏʟᴅsᴘᴇᴇᴅ 写
max(h)的方式很糟糕。