python heapq模块使用
Python内置的heapq模块
Python3.4版本中heapq包含了几个有用的方法:
heapq.heappush(heap,item):将item,推入heap
>>> items = [1,2,9,7,3]
>>> heapq.heappush(items,10)
>>> items
[1, 2, 9, 7, 3, 10]
>>>
heapq.heappop(heap):将heap的最小值pop出heap,heap为空时报IndexError错误
>>> heapq.heappop(items)#heap在pop时总是将最小值首先pop出
1
>>> items
[2, 3, 9, 7, 10]
>>>
heapq.heappushpop(heap,item):pop出heap中最小的元素,推入item
>>> items
[2, 3, 9, 7, 10]
>>> heapq.heappushpop(items,11)
2
>>> items
[3, 7, 9, 11, 10]
>>>
heapq.heapify(x):将list X转换为heap
>>> nums = [1,10,9,8]
>>> heap = list(nums)
>>> heapq.heapify(heap)
>>> heap
[1, 8, 9, 10]
>>>
heapq.heapreplace(heap,item):pop出最小值,推入item,heap的size不变
>>> heap
[1, 8, 9, 10]
>>> heapq.heapreplace(heap,100)
1
>>> heap
[8, 10, 9, 100]
>>
heapq.merge(*iterable):将多个可迭代合并,并且排好序,返回一个iterator
>>> heap
[8, 10, 9, 100]
>>> heap1 = [10,67,56,80,79]
>>> h = heapq.merge(heap,heap1)
>>> list(h)
[8, 10, 9, 10, 67, 56, 80, 79, 100]#需要 说明的是这里所谓的排序不是完全排序,只是两个list对应位置比较,
#将小的值先push,然后大的值再与另外一个list的下一个值比较
heapq.nlargest(n,iterable,key):返回item中大到小顺序的前N个元素,key默认为空,可以用来指定规则如:function等来处理特定的排序
itemsDict=[
{'name':'dgb1','age':23,'salary':10000},
{'name':'dgb2','age':23,'salary':15000},
{'name':'dgb3','age':23,'salary':80000},
{'name':'dgb4','age':23,'salary':80000}
]
itemsDictlarge = heapq.nlargest(3,itemsDict,lambda s:s['salary'])
print(itemsDictlarge)
[{'name': 'dgb3', 'age': 23, 'salary': 80000}, {'name': 'dgb4', 'age': 23, 'salary': 80000}, {'name': 'dgb2', 'age': 23, 'salary': 15000}]
如果没有指定key,那么就按照第一个字段来排序
heapq.nsmallest(n,iterable,key):返回item中小到大顺序的前N个元素,key默认为空,可以用来指定规则如:function等来处理特定的排序
这个函数的用法与上一个nlargest是一样的。
To create a heap, use a list initialized to[], or you can transform a populated list into a heap via functionheapify().
创建heap可以通过创建list,和使用heapify方法来实现。
---------------------
from:https://blog.csdn.net/chuan_day/article/details/73554861
python heapq模块使用的更多相关文章
- Python heapq 模块的实现 - A Geek's Page
Python heapq 模块的实现 - A Geek's Page Python heapq 模块的实现
- Python heapq模块
注意,默认的heap是一个小顶堆! heapq模块提供了如下几个函数: heapq.heappush(heap, item) 把item添加到heap中(heap是一个列表) heapq.heappo ...
- Python常用数据结构之heapq模块
Python数据结构常用模块:collections.heapq.operator.itertools heapq 堆是一种特殊的树形结构,通常我们所说的堆的数据结构指的是完全二叉树,并且根节点的值小 ...
- python标准库:collections和heapq模块
http://blog.csdn.net/pipisorry/article/details/46947833 python额外的数据类型.collections模块和heapq模块的主要内容. 集合 ...
- python 中的堆 (heapq 模块)应用:Merge K Sorted Lists
堆是计算机科学中一类特殊的数据结构的统称.堆通常是一个可以被看做一棵树的数组对象.在队列中,调度程序反复提取队列中第一个作业并运行,因为实际情况中某些时间较短的任务将等待很长时间才能结束,或者某些不短 ...
- python 关于heapq模块的随笔
heapq模块提供了很多高级功能可以通过help(heapq)查看详细文档: 要点: 1优先级队列让我们可以按照重要程度来处理元素,而不是先进先出 2使用heapq可以应对长列表,因为heap不是复杂 ...
- [Python] heapq简介
[Python] heapq简介 « Lonely Coder [Python] heapq简介 judezhan 发布于 2012 年 8 月 8 日 暂无评论 发表评论 假设你需要维护一个列表,这 ...
- python --- queue模块使用
1. 什么是队列? 学过数据结构的人都知道,如果不知道队列,请Google(或百度). 2. 在python中什么是多生产者,多消费模型? 简单来说,就是一边生产(多个生产者),一边消费(多个消费者) ...
- python标准模块
sys模块 这是一个跟python解释器关系密切的标准库.它提供了一些和python解释器操作密切的属性和函数. sys中常用的函数和属性: sys.argv: sys.argv是专门用来向pytho ...
随机推荐
- git pull 提示 There is no tracking information for the current branch
在执行git pull的时候,提示当前branch没有跟踪信息: git pull There is no tracking information for the current branch. P ...
- React Native 炫酷的动画库 实现任何AE动画 lottie-react-native
lottie-react-native 传送门 1.npm i --save lottie-react-native 2.react-native link lottie-ios 3.react-na ...
- MongoDB 对象操作
对象插入 >db.col.insert({title: 'MongoDB 教程', description: 'MongoDB 是一个 Nosql 数据库', by: 'xxx', url: ' ...
- Kali Linux 更新源 操作完整版教程
一.查看kali系统的更新源地址文件 命令: vim /etc/apt/sources.list 上面这是kali官方的更新源: 拓展知识: 一个完整的源包括:deb 和 deb-src:上图源地址是 ...
- vim的简单使用
vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一定会对这个编辑器失去兴趣的.下面的文章翻译自<Learn Vim Progress ...
- Pg MySQL
https://blog.csdn.net/tiandao2009/article/details/79839037 1架构 2对sql支持的完备性 3join Nest join , 4表分区 p ...
- Codeforces 981 E - Addition on Segments
E - Addition on Segments 思路: dp dp[i]表示构成i的区间的右端点 先将询问按r排序 然后,对于每次询问,每次枚举 i 从 n-x 到 1,如果dp[i] >= ...
- Asp.net core 学习笔记 (授权)
更新 : 2018-11-24 记入一些思考 asp.net core + identity 的权限是这样的 user = 1 个登入账号 role = 1 个角色 (类似于公司里的一个职位) cla ...
- Top 命令解析
TOP是一个动态显示过程,即可以通过用户按键来不断刷新当前状态.如果在前台执行该命令,它将独占前台,直到用户终止该程序为止.比较准确的说,top命令提供了实时的对系统处理器的状态监视.它将显示系统中C ...
- TCHART类型
private Steema.TeeChart.Styles.Pie pieSeries1; private Steema.TeeChart.Styles.Pie pieSeries2; privat ...