笔记-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. asp.net MVC 中枚举创建下拉列表?

    我将尝试使用 Html.DropDownList 扩展方法,但不能找出如何使用它的枚举. 让我们说我有一个这样的枚举: public enum ItemTypes { Movie = 1, Game ...

  2. JMeter测试TCP服务器遇到的一个奇怪问题

    今天工作需要测TCP服务器的压力,因为tsung测试TCP需要写的脚本实在头大,于是换了JMETER来搞压力测试.在实际测试的过程中,遇到了一个很奇怪的问题,就是发了数据包以后,JMeter不停地报5 ...

  3. python_3 :用python微信跳一跳

    [学习使用他人代码] 2018年01月21日 19:29:02 独行侠的守望 阅读数:319更多 个人分类: Python 编辑 版权声明:本文为博主原创文章,转载请注明文章链接. https://b ...

  4. 私有npm下载资源

    私有npm库下载资源需要用户名和密码,这个需要创建npm库的人提供. 使用方法: npm login --registry=仓库地址 Username: 用户名 Password: 密码 Email: ...

  5. genlist -s 192.168.21.\*

    显示网段192.168.21中可用的主机.

  6. Reduce侧连接

    1.reduce side join 在reduce端进行表的连接,该方法的特点就是操作简单,缺点是map端shffule后传递给reduce端的数据量过大,极大的降低了性能 连接方法: (1)map ...

  7. 谷歌chrome://chrome-urls/

    查看DNS解析时间 1 chrome://dns/ 查看DNS解析的地址 1 chrome://net-internals/#dns 更多功能请参考 1 chrome://chrome-urls/ 以 ...

  8. Nginx+Keepalived双主轮询负载均衡

    双主模式使用两个VIP,前段有2台服务器,互为主从,两台服务器同时工作,不存在资源浪费情况.同时在前端的DNS服务器对网站做多条A记录,实现了Nginx的负载均衡,当一台服务器故障时候,资源会转移到另 ...

  9. IOS 监听slider滑动

    // 监听slider滑动 - (IBAction)valueChnage:(UISlider *)sender; @property (weak, nonatomic) IBOutlet NJVie ...

  10. Incorrect key file for table './xx_db/xx_table.MYI'; try to repair it

    解决办法: 可以先运行 CHECK TABLE 表名 检查下是否存在错误. 然后运行 REPAIR TABLE 表名 进行修复.