笔记-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的更多相关文章

  1. The Python Standard Library

    The Python Standard Library¶ While The Python Language Reference describes the exact syntax and sema ...

  2. Python语言中对于json数据的编解码——Usage of json a Python standard library

    一.概述 1.1 关于JSON数据格式 JSON (JavaScript Object Notation), specified by RFC 7159 (which obsoletes RFC 46 ...

  3. Python Standard Library

    Python Standard Library "We'd like to pretend that 'Fredrik' is a role, but even hundreds of vo ...

  4. 《The Python Standard Library》——http模块阅读笔记1

    官方文档:https://docs.python.org/3.5/library/http.html 偷个懒,截图如下: 即,http客户端编程一般用urllib.request库(主要用于“在这复杂 ...

  5. 《The Python Standard Library》——http模块阅读笔记2

    http.server是用来构建HTTP服务器(web服务器)的模块,定义了许多相关的类. 创建及运行服务器的代码一般为: def run(server_class=HTTPServer, handl ...

  6. 《The Python Standard Library》——http模块阅读笔记3

    http.cookies — HTTP state management http.cookies模块定义了一系列类来抽象cookies这个概念,一个HTTP状态管理机制.该模块支持string-on ...

  7. Python Standard Library 学习(一) -- Built-in Functions 内建函数

    内建函数列表 Built-in Functions abs() divmod() input() open() staticmethod() all() enumerate() int() ord() ...

  8. [译]The Python Tutorial#11. Brief Tour of the Standard Library — Part II

    [译]The Python Tutorial#Brief Tour of the Standard Library - Part II 第二部分介绍更多满足专业编程需求的高级模块,这些模块在小型脚本中 ...

  9. C++11新特性——The C++ standard library, 2nd Edition 笔记(一)

    前言 这是我阅读<The C++ standard library, 2nd Edition>所做读书笔记的第一篇.这个系列基本上会以一章一篇的节奏来写,少数以C++03为主的章节会和其它 ...

  10. [译]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模块为与操作系统 ...

随机推荐

  1. &&运算符和||运算符的优先级问题 专题

    public class SyntaxTest { @Test public void test() { System.out.println(true || true && fals ...

  2. .net中Response.End() 和Response.Redirect("http://dotnet.aspx.cc");

    问:什么情况下需要Response.End()语句,加这句有什么好处 答: 首先你要理解Response.End()的意思,它的意思是终止执行下面的语句!但有时不加和加上都一样,但还要加上好,为什么呢 ...

  3. spring的工厂方法

    http://blog.csdn.net/nvd11/article/details/51542360

  4. cf1040E. Network Safety(并查集)

    题意 题目链接 一张图,n个点,m条边,每个点有个权值x,x<=1e18.如果一条边的两个端点不一样,那么这条边是安全的,开始时所有边都是安全的. 现在有一个病毒y,病毒可以入侵任意的点,入侵一 ...

  5. vscode 常用插件安装

    设置中文语言使用快捷键[Ctrl+Shift+P],弹出的搜索框中输入[configure language],然后选择搜索出来的[Configure Display Language],locale ...

  6. 编译64位geos库的经验总结

    作者:朱金灿 来源:http://blog.csdn.net/clever101 使用CMake生成Win64的解决方案后,使用VS2010打开这个解决方案,然后 在"C/C++" ...

  7. 彻底解决Android 应用方法数不能超过65K的问题

    作为一名Android开发者,相信你对Android方法数不能超过65K的限制应该有所耳闻,随着应用程序功能不断的丰富,总有一天你会遇到一个异常: Conversion to Dalvik forma ...

  8. lintcode五道题

    1.二叉树的最大深度 最大深度为根节点到最远叶子节点的距离为最大深度,于是可以先找到根节点到叶子节点最大的距离,过程就可以分为左子树 和右子树分别进行来求左.右子树的最大深度lh=height(roo ...

  9. 访问FTP站点下载文件,提示“当前的安全设置不允许从该位置下载文件”的解决方案

    访问FTP站点下载文件,提示“当前的安全设置不允许从该位置下载文件”的解决方案: 打开客戶端浏览器--工具---internet-安全-自定义级别-选择到低到中低. 然后点受信任站点,把你要访问的站点 ...

  10. 工作流性能优化(敢问activiti有扩展性?)(2)

    2015/4/17 粗略看了activiti的sql的,在ativity engine包里边: 没什么头绪,先用excel记录数据量少的时候本机的性能情况:   不打印hibernate的sql:一刷 ...