实现优先级队列 --heapq模块
以给定的优先级对元素进行排序,每次pop删除优先级最高的
# coding=utf-8
# example.py
#
# Example of a priority queue import heapq class PriorityQueue:
def __init__(self):
self._queue = []
self._index = 0 def push(self, item, priority):
heapq.heappush(self._queue, (-priority, self._index, item)) #在这里就是依据优先级的负数作排序依据,最小的在左边,是第0个值
self._index += 1 #当优先级一样是,index按照小的先输出,保证了先进先出 def pop(self):
return heapq.heappop(self._queue)[-1] #返回item # Example use
class Item:
def __init__(self, name):
self.name = name
def __repr__(self):
return 'Item({!r})'.format(self.name)#!后面可以加s r分别对应str() repr() ,和%用法类似,s没括号,r有括号 q = PriorityQueue()
q.push(Item('foo'), 1)
q.push(Item('bar'), 5)
q.push(Item('spam'), 4)
q.push(Item('grok'), 1) print("Should be bar:", q.pop())
print("Should be spam:", q.pop())
print("Should be foo:", q.pop())
print("Should be grok:", q.pop())
a=[5,8,9]
b=[5,1,2]
c=[4,6,7]
ss=[]
heapq.heappush(ss,a)#是按照a中序列的第一个作排序依据的
heapq.heappush(ss,b)
heapq.heappush(ss,c)
print ss
s=heapq.heappop(ss)[-1]
print s,ss
s=heapq.heappop(ss)[-1]
print s,ss
s=heapq.heappop(ss)[-1]
print s,ss
结果:
H:\Python27_64\python.exe H:/myfile/python-cookbook-master/src//implementing_a_priority_queue/example.py
('Should be bar:', Item('bar'))
('Should be spam:', Item('spam'))
('Should be foo:', Item('foo'))
('Should be grok:', Item('grok'))
[[, , ], [, , ], [, , ]]
[[, , ], [, , ]]
[[, , ]]
[] 进程已结束,退出代码0
实现优先级队列 --heapq模块的更多相关文章
- 【python cookbook】【数据结构与算法】5.实现优先级队列
问题:要实现一个队列,它能够以给定的优先级对元素排序,且每次pop操作时都会返回优先级最高的那个元素: 解决方案:采用heapq模块实现一个简单的优先级队列 # example.py # # Exam ...
- Python常用数据结构之heapq模块
Python数据结构常用模块:collections.heapq.operator.itertools heapq 堆是一种特殊的树形结构,通常我们所说的堆的数据结构指的是完全二叉树,并且根节点的值小 ...
- 用Python实现数据结构之优先级队列
优先级队列 如果我们给每个元素都分配一个数字来标记其优先级,不妨设较小的数字具有较高的优先级,这样我们就可以在一个集合中访问优先级最高的元素并对其进行查找和删除操作了.这样,我们就引入了优先级队列 这 ...
- [PY3]——实现一个优先级队列
import heapq class PriorityQueue: def __init__(self): self._queue=[] self._index=0 def push(self,ite ...
- python3-开发进阶 heapq模块(如何查找最大或最小的N个元素)
一.怎样从一个集合中获得最大或者最小的 N 个元素列表? heapq 模块有两个函数:nlargest() 和 nsmallest() 可以完美解决这个问题. import heapq nums = ...
- Python 单向队列Queue模块详解
Python 单向队列Queue模块详解 单向队列Queue,先进先出 '''A multi-producer, multi-consumer queue.''' try: import thread ...
- Python之实现一个优先级队列
问题 怎样实现一个按优先级排序的队列? 并且在这个队列上面每次 pop 操作总是返回优先级最高的那个元素 解决方案 下面的类利用 heapq 模块实现了一个简单的优先级队列: import heapq ...
- python 关于heapq模块的随笔
heapq模块提供了很多高级功能可以通过help(heapq)查看详细文档: 要点: 1优先级队列让我们可以按照重要程度来处理元素,而不是先进先出 2使用heapq可以应对长列表,因为heap不是复杂 ...
- python标准库:collections和heapq模块
http://blog.csdn.net/pipisorry/article/details/46947833 python额外的数据类型.collections模块和heapq模块的主要内容. 集合 ...
随机推荐
- 把一个文件中所有文件名或者文件路径读取到一个txt文件,然后在matlab中读取
链接: http://blog.csdn.net/dreamgchuan/article/details/51113295 dir /on/b/s 这个读取的是这样的格式:
- Centos7中使用ipset
1.禁用firewalld systemctl stop firewalld systemctl disable firewalld 2.安装ipset yum -y install ipse ...
- C语言 结构体作为参数和返回值使用
方案一:结构体变量作为参数,进行传值. 编译器需要拷贝,不影响origin value,使用成员操作符(.)直接访问 /**************************************** ...
- 51nod 1629 B君的圆锥
1629 B君的圆锥 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 B君要用一个表面积为S的圆锥将白山云包起来. B君希望包住的白山云体积尽量 ...
- uva 557 Burger
https://vjudge.net/problem/UVA-557 题意: n个人,n/2个牛肉煲,n/2个鸡肉堡 每次抛硬币,根据正反决定每个人吃什么汉堡 如果某一个汉堡被选完了,就不抛了 问最后 ...
- Python学习笔记(四十八)POP3收取邮件
收取邮件就是编写一个MUA作为客户端,从MDA把邮件获取到用户的电脑或者手机上.收取邮件最常用的协议是POP协议,目前版本号是3,俗称POP3. Python内置一个poplib模块,实现了POP3协 ...
- Vue中使用vux的配置
一.根据vux文档直接安装,无需手动配置 npm install vue-cli -g // 如果还没安装 vue init airyland/vux2 my-project // 创建名为 my-p ...
- python Multiprocessing 多进程应用
在运维工作中,经常要处理大量数据,或者要跑一些时间比较长的任务,可能都需要用到多进程,不管是管理端下发任务,还是客户端执行任务,如果服务器配置还可以,跑多进程还是挺能解决问题的 Multiproces ...
- 前端开发进阶:推荐的 CSS 书写规范
写了这么久的CSS,但大部分前端er都没有按照良好的CSS书写规范来写CSS代码,这样会影响代码的阅读体验,这里总结一个CSS书写规范.CSS书写顺序供大家参考,这些是参考了国外一些文章以及我的个人经 ...
- 【CodeForces】908 D. New Year and Arbitrary Arrangement
[题目]Good Bye 2017 D. New Year and Arbitrary Arrangement [题意]给定正整数k,pa,pb,初始有空字符串,每次有pa/(pa+pb)的可能在字符 ...