实现优先级队列 --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模块的主要内容. 集合 ...
随机推荐
- poj2115 C Looooops
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29262 Accepted: 8441 Descr ...
- 杨辉三角之c实现任意行输出
#include<stdio.h> #include<stdlib.h> int** fmalloc(int n){ int** array; //二维指针 int i; ar ...
- 基于packstack的openstack单节点安装
一.安装源处理 1.更新base源为网易的源 cd /etc/yum.repos.d/ wget http://mirrors.163.com/.help/CentOS6-Base-163.repo ...
- C++ string功能补充(类型互转,分割,合并,瘦身)
int StringUtil::intFromString(string data) { //NOTE atoi是非标准C函数 return atoi(data.c_str()); } string ...
- ACM选修HUST1058(市赛题) Lucky Sequence 同余定理
Description Edward 得到了一个长度为 N 的整数序列,他想找出这里面有多少个“幸运的”连续子序列.一个连续子序列被称为“幸运的”,当且仅当该子序列内的整数之和恰好是 K 的 ...
- HEXO与Github.io搭建个人博客
HEXO与Github.io搭建个人博客 HEXO搭建 HEXO是基于Node.JS的一款简单快速的博客框架,能够支持多线程,支持markdown,可以将生成的静态网页发布到github.io以 ...
- ② 设计模式的艺术-02.简单工厂(Simple Factory)模式
工厂模式 实现了创建者和调用者的分离. 详细分类:简单工厂模式.工厂方法模式.抽象工厂模式 面向对象设计的基本原则 OCP(开闭原则,Open-Closed Principle):一个软件的实体应当对 ...
- 【leetcode 简单】第六题 有效的括号
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空字符串可被认 ...
- 垂直水平居中--css3
在移动前端制作中,很多新的css3特性能够帮助我们更好的制作.例如这个垂直水平居中问题,就有一个简单的代码可以解决: 利用CSS3的transform:translate .center{ width ...
- CSS Sprite笔记
1. 什么是CSS Sprite 将一些碎小的图片拼接为一张大点的图片来使用,目的是为了减少浏览器的http请求次数以提高网页的加载速度.每次请求图片都需要跟服务器建立一次连接,并且浏览器的最大并发请 ...