第一个元素一定是最小的,如果每次只想拿到列表的最小值,不想整个列表排序,可以通过不断返回最小值的方法实现

from heapq import heapify, heappop, heappush
data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]
heapify(data)                      # rearrange the list into heap order
heappush(data, -5)                 # add a new entry
heappush(data, -66)
c=[heappop(data) for i in range(3)]  # fetch the three smallest entries
print(c)
print(data)

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-02
  • 2022-01-15
  • 2022-12-23
  • 2021-10-07
  • 2021-09-13
猜你喜欢
  • 2022-03-04
  • 2021-11-18
  • 2022-01-12
  • 2021-11-01
  • 2021-04-29
  • 2022-01-15
相关资源
相似解决方案