笔记-python-standard library-8.5.heapq
笔记-python-standard library-8.5.heapq
1. heapq-heap queue algorithm
源码:Lib/heapq.py
this module provides an implementation of the heap queue algorithm.
heap are binary trees for which every parent node has a value less than or equal to any of its children.
堆(英语:heap)是计算机科学中一类特殊的数据结构的统称。堆通常是一个可以被看做一棵树的数组对象。堆总是满足下列性质:
堆中某个节点的值总是不大于或不小于其父节点的值;
堆总是一棵完全二叉树。
模块中的API与教科书中的有两点不同:
1. 索引从0开始,在寻找子节点时需要注意;
2. pop() method returns the smallest item,not the largest。简单来说,是一个最小堆。
一般情况下,heap[0]是整个序列的最小值,可以使用heap.sort()来排序。
创建堆可以使用[],也可以使用heapify()来初始化一个列表为堆。
functions:
heapq.heappush(heap, item)将一个值推进堆,如果推进的是最小值,也会改变后面元素的排序。
heapq.heappop(heap) 将heap[0]抛出,再排序保证heap[0]是最小值。
heapq.heappushpop(heap, item) 先推进堆,再抛出一个最小值,上面两个的综合。
heapq.heapify(x) 对列表x进行堆化,仅保证heap[0]是最小值,其它部分顺序不保证。
hepq.heapreplace(heap, item) 抛出并返回最小值,推进一个新值。
heapq.merge(*iterables, key=None, reverse=False)
将多个列表合并并堆化,返回一个迭代器。
heapq.nlargest(n, iterable, key=None)
返回一个列表,其中包含给出的最大的n个元素。
heapq.nsmallest(n, iterable, key=None)同上,不过是最小的n个元素。
因为heapq默认最小值在前,因此nsmallest()效率较高,而nlargest()效率较低。
笔记-python-standard library-8.5.heapq的更多相关文章
- The Python Standard Library
The Python Standard Library¶ While The Python Language Reference describes the exact syntax and sema ...
- Python语言中对于json数据的编解码——Usage of json a Python standard library
一.概述 1.1 关于JSON数据格式 JSON (JavaScript Object Notation), specified by RFC 7159 (which obsoletes RFC 46 ...
- Python Standard Library
Python Standard Library "We'd like to pretend that 'Fredrik' is a role, but even hundreds of vo ...
- 《The Python Standard Library》——http模块阅读笔记1
官方文档:https://docs.python.org/3.5/library/http.html 偷个懒,截图如下: 即,http客户端编程一般用urllib.request库(主要用于“在这复杂 ...
- 《The Python Standard Library》——http模块阅读笔记2
http.server是用来构建HTTP服务器(web服务器)的模块,定义了许多相关的类. 创建及运行服务器的代码一般为: def run(server_class=HTTPServer, handl ...
- 《The Python Standard Library》——http模块阅读笔记3
http.cookies — HTTP state management http.cookies模块定义了一系列类来抽象cookies这个概念,一个HTTP状态管理机制.该模块支持string-on ...
- Python Standard Library 学习(一) -- Built-in Functions 内建函数
内建函数列表 Built-in Functions abs() divmod() input() open() staticmethod() all() enumerate() int() ord() ...
- [译]The Python Tutorial#11. Brief Tour of the Standard Library — Part II
[译]The Python Tutorial#Brief Tour of the Standard Library - Part II 第二部分介绍更多满足专业编程需求的高级模块,这些模块在小型脚本中 ...
- C++11新特性——The C++ standard library, 2nd Edition 笔记(一)
前言 这是我阅读<The C++ standard library, 2nd Edition>所做读书笔记的第一篇.这个系列基本上会以一章一篇的节奏来写,少数以C++03为主的章节会和其它 ...
- [译]The Python Tutorial#10. Brief Tour of the Standard Library
[译]The Python Tutorial#Brief Tour of the Standard Library 10.1 Operating System Interface os模块为与操作系统 ...
随机推荐
- C 碎片三 运算符与表达式
一.算术运算符 算术运算符:+. -. *. /. %等 加:+ 减: - 乘: * 除: / 除数不能为0 模:% 参与模运算的数据不能为小数 二.赋值运算符 赋值运算符:= 作用: ...
- chroot 的应用
http://www.williamlong.info/archives/3864.html http://my.oschina.net/u/1590519/blog/342576
- iOS开发ReactiveCocoa学习笔记(三)
RAC常用用法: 1.监听按钮的点击事件: UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame ...
- jQuery动态添加元素,并提交json格式数据到后台
参考:https://www.cnblogs.com/shj-com/p/7878727.html 下载 下载该插件的地址是:http://www.bootcdn.cn/jquery.serializ ...
- ifream页面弹出框遮盖层覆盖父页面
1.首先找到子页面上遮罩层的id, 2.然后再父页面编写个js方法 function shade() { $(".layui-layer-shade").height($(wind ...
- JVM虚拟机 - Class类文件结构
概述 Class文件是一组以8位字节为基础单位的二进制流,各个数据项目严格按照顺序紧凑地排列在Class文件之中,中间没有添加任何分隔符,这使得整个Class文件中存储的内容几乎都是程序运行的必要数据 ...
- linux修改系统时间为北京时间(CentOS)
删除本地时间 rm -rf /etc/localtime 设置时区为上海 ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 利用date查看 ...
- C++中构造函数的写法
class Circle { public: Circle(float r); private: float radius; }; Circle::Circle(float r) { radius = ...
- [荐]推荐一个shell学习的网站
最近再用shell脚本,发现一个脚本学习的网站,非常好用,特此推荐一下. shell学习网站链接:http://c.biancheng.net/cpp/shell/
- isset或array_key_exists,检查数组键是否存在
今天在导出报表的时候遇到了一个问题,undefined index:pid,然后就纳闷了,我的数组里面根本就没有pid,为什么会出现这个错误呢,我遍历了一下数组,发现果然有pid这个键,奇怪呀,我有做 ...