python没有实现专门的堆数据结构,但是提供了一系列方法,可以操作在list上,实现堆的功能

import heapq
# 将x压入堆中
heapq.heappush(heap, x)   
# 从堆中弹出最小的元素                                     
heapq.heappop(heap)    
# 让列表具备堆特征                                  
heapq.heapify(heap) 
# 弹出最小的元素,并将x压入堆中                                      
heapq.heapreplace(heap, x)  
# 返回iter中n个最大的元素                          
heapq.nlargest(n, iter) 
# 返回iter中n个最小的元素                                      
heapq.nsmallest(n, iter)                                   

# 从一个list中获取最大的元素
max_column_data = heapq.nlargest(1, temp_list, key=lambda item: len(item))

 

相关文章:

  • 2021-12-29
  • 2022-12-23
  • 2021-09-10
  • 2021-12-14
  • 2021-07-09
  • 2021-12-31
  • 2021-07-13
  • 2021-12-13
猜你喜欢
  • 2021-10-29
  • 2021-12-30
  • 2021-06-19
  • 2021-05-26
  • 2021-12-23
  • 2021-05-21
相关资源
相似解决方案