笔记-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模块为与操作系统 ...
随机推荐
- jquery调用asp.net 页面后台的实现代码
先创建一个aspx页面编写一个客户端控件<input type="button" id="AjaxDemo" value="AjaxDemo&q ...
- contenttype组件、Django缓存机制以及跨域请求
1 昨日回顾 版本控制 *** (1)url=127.0.0.1/course/?version=v100000 1 versioning_class=QueryParameterVersioning ...
- It's the loneliest feeling not to know who you are.
It's the loneliest feeling not to know who you are.最孤独的感觉莫过于不知道自己是谁.
- ubuntu安装robo3t
直接在官网下载 解压文件(使用命令 tar -zxvf robo3t-1.2.1-linux-x86_64-3e50a65.tar.gz) 打开解压后的文件,进入bin文件,直接在终端运行 ./rob ...
- Eucalyptus-利用镜像启动一个Windows Server 2008r2实例
1.前言 使用kvm制作Eucalyptus镜像(Windows Server 2008r2为例)——http://www.cnblogs.com/gis-luq/p/3990792.html 上一篇 ...
- 微信iOS端无法执行jquery on()方法
微信iOS端无法执行jquery on()方法,click方法可以, 如下代码是不会执行的: $(function(){ $('body').on('click','.cka',function(){ ...
- Android商城开发系列(十四)—— 设置监听RecyclerView的位置
在前面的博客中有讲到过点击一个图片按钮控制RecyclerView的滚动到顶部位置的效果,但是那个图片按钮一直处在一个显示的状态,今天我们来改造一下那个地方,我们要实现的效果是:一开始打开的时候看不到 ...
- 解决Android Studio和Android SDK Manager无法在线更新的问题[转]
升级时提示 Connection failed. Please check your network connection and try again 修改安装目录下bin\studio.exe.vm ...
- C++ list类详解
转自:http://blog.csdn.net/whz_zb/article/details/6831817 双向循环链表list list是双向循环链表,,每一个元素都知道前面一个元素和后面一个元素 ...
- Win7系统如何设置FTP详细过程
1.安装FTP组件 点击:控制面板—>程序和功能—>打开或关闭Windows功能.勾选“FTP服务器”及“FTP服务”“FTP扩展性”,点击“确定”,安装FTP组件. 2.添加FTP站点 ...