2025年03月01日
如何使用python3中的heapq模块?
模块安装:
pip install heapq
模块函数:
nlargest()
nsmallest()
应用实例:
实现堆排序
from heapq import *
def heap_sort(iterable):
h = []
for value in iterable:
heappush(h, value)
return [heappop(h) for _ in range(len(h))]
if __name__
模块使用