heapq.heappush(heap,item):将item,推入heap
#堆内数据不一定是有序的,但是如果每个数据都是一次次push进来的,那么数据将会是有序的
>>> items = [1,2,9,7,3]
>>> heapq.heappush(items,10)
>>> items
[1, 2, 9, 7, 3, 10]
>>> heapq.heappop(items)

#heap在pop时总是将最小值首先pop出 1 >>> items [2, 3, 9, 7, 10]
 heapq.heapreplace(heap,item):pop出最小值,推入item,heap的size不变

>>> heap
[1, 8, 9, 10]
>>> heapq.heapreplace(heap,100)
1
>>> heap
[8, 10, 9, 100]

 

相关文章:

  • 2021-07-18
  • 2021-07-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-05
猜你喜欢
  • 2021-06-04
  • 2021-10-08
  • 2022-02-05
  • 2021-08-13
  • 2022-12-23
  • 2021-06-23
相关资源
相似解决方案