python 的内置模块堆 heapq

发布时间:2020-04-08 12:44:18 作者:ading2016
来源:网络 阅读:258

In [1]: import heapq

In [2]: h = []

In [3]: h.append(1)

In [4]: h.append(2)

In [5]: h.append(3)

In [6]: h.append(8)

In [7]: h.append(5)

In [8]: h.append(0)
#用heapq 生成一个最小堆
In [9]: heapq.heapify(h)

In [10]: heapq.heappop()

In [11]: heapq.heappop(h)
Out[11]: 0

In [12]: heapq.heappop(h)
Out[12]: 1

In [13]: heapq.heappop(h)
Out[13]: 2

In [14]: heapq.heappop(h)
Out[14]: 3

In [15]: heapq.heappop(h)
Out[15]: 5

In [16]: heapq.heappop(h)
Out[16]: 8

推荐阅读:
  1. 常见python内置数据结构算法
  2. python中有没有堆

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python heapq

上一篇:定制自己的shell登录脚本

下一篇:django数据查询优化annotate和aggregate

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》