笔记-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. hibernate课程 初探单表映射3-1 单一主键

    本节简介: 1 单一主键的两种赋值方式:手动赋值(assigned)和自动赋值(native) 2 mysql和oracle赋值的不同形式 3 demo 2 native由底层数据库生成标识符,如果是 ...

  2. iOS .Crash文件分析处理办法 (利用symbolicatecrash工具处理)

    崩溃分析方式:命令行解析Crash文件 通过Mac自带的命令行工具解析Crash文件需要具备三个文件 symbolicatecrash,Xcode自带的崩溃分析工具,使用这个工具可以更精确的定位崩溃所 ...

  3. 远程登录事件ID

    4672.4624 删除本机记录 HKEY_CURRENT_USER \ Software\Microsoft  \ Terminal ServerClientDefault: 删除“此电脑\文档”下 ...

  4. linux 命令——25 linux文件属性详解

    Linux 文件或目录的属性主要包括:文件或目录的节点.种类.权限模式.链接数量.所归属的用户和用户组.最近访问或修改的时间等内容.具体情况如下: 命令: ls -lih 输出: [root@loca ...

  5. convert命令

    可以修改图片的分辨率 convert -resize 600×600 src.jpg dst.jpg src.jpg是你要修改的图片的名字 dst.jpg是新生成的图片名字

  6. python_9_for

    #1 for i in range(10):#默认从0开始,步长为1 print("loop",i) #2 for i in range(0,10,1):#步长为1 print(& ...

  7. OneNote- 面经

  8. java基础面试题:写clone()方法时,通常都有一行代码,是什么?

    clone()方法 与new constructor()构造器创建对象不同 是克隆一个新的对象 package com.swift; public class Clone_Test { public ...

  9. .NET下寄宿于控制台的HTTPS监听

    附上原文链接:https://blogs.msdn.microsoft.com/jpsanders/2009/09/29/how-to-walkthrough-using-httplistener-o ...

  10. Springboot 入门创建hello world1!

    1.首先使用工具是Eclipse,安装插件,点击“Help”-“Eclipse Marketplace...”, 一步步直接Ok,等待安装完成 2.创建Springboot项目 到此 就创建成功了 3 ...