python堆模块_python内置堆模块
heapq
python内置heapq模块,通过import heapq导入。
heapq模块是用于堆实现优先队列。我们知道队列是先进先出(FIFO),
heapq中的优先队列指的是不论谁先进,最小的先出或者最大的先出。
# 需要注意的是heapq的堆是小根堆。
0
1 2
3 4 5 6
7 8 9 10 11 12 13 14
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
# 堆顶位置的元素最小,heapq和python一致,下标索引从0开始。
# heapq提供的主要API, Usage:
heap = [] # creates an empty heap
heappush(heap, item) # pushes a new item on the heap
item = heappop(heap) # pops the smallest item from the heap
item = heap[0] # smallest item on the heap without popping it
heapify(x) # transforms list into a heap, in-place, in linear time
item = heapreplace(heap, item) # pops and returns smallest item, and adds
# new item; the heap size is unchanged
heapq实现列表排序
基本排序流程
# -*- coding: utf-8 -*-
# created by X. Liu on 2020/3/14
import heapq
import random
li = [i for i in range(10)]
random.shuffle(li)
print('排序前', li)
# step1: 建堆(小根堆)
heapq.heapify(li)
print('小根堆', li)
# step2: 排序
# heapq.heappop()方法弹出堆顶元素,即最小的元素
new_li = []
for i in range(10):
new_li.append(heapq.heappop(li))
print('排序后', new_li)
# output:
排序前 [3, 4, 9, 0, 5, 1, 6, 2, 8, 7]
小根堆 [0, 2, 1, 3, 5, 9, 6, 4, 8, 7]
排序后 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
堆排序实现版本2 (不考虑时间复杂度)
# 借助 heapq.heappush() & heapq.heappop()
# heappush(h, item) # Push the value item onto the heap, maintaining the heap invariant.
import heapq
def heap_sort(li):
h = []
# 建小根堆
for i in li:
heapq.heappush(h, i)
# 依次弹出最小元素构成有序列表,切片回li
li[:] = [heapq.heappop() for i in range(len(li))]
对比分析
heapq的heapify建堆函数和我们自己实现的建堆函数大同小异。我们自己的sift函数一步到位(实现堆顶元素选位的过程);heapify中使用两步判断比较,其实不如我们自己写的sift好。
heappop函数实现每次弹出堆顶元素,弹出后再将整个堆调整为一个新的小根堆。它的目的是为了实现优先队列,所以才会这样设计。我们如果希望借助它实现列表排序,只能手动排序。
补充
heap的值可以是元组
>>> h = []
>>> heappush(h, (5, 'write code'))
>>> heappush(h, (7, 'release product'))
>>> heappush(h, (1, 'write spec'))
>>> heappush(h, (3, 'create tests'))
>>> heappop(h)
(1, 'write spec')
heapq.heapify(x)底层代码实现
def heapify(x):
n = len(x)
for i in reversed(range(n//2)):
_siftup(x, i)
def _siftup(heap, pos):
endpos = len(heap)
startpos = pos
newitem = heap[pos]
# Bubble up the smaller child until hitting a leaf.
childpos = 2*pos + 1 # leftmost child position
while childpos < endpos:
# Set childpos to index of smaller child.
rightpos = childpos + 1
if rightpos < endpos and not heap[childpos] < heap[rightpos]:
childpos = rightpos
# Move the smaller child up.
heap[pos] = heap[childpos]
pos = childpos
childpos = 2*pos + 1
# The leaf at pos is empty now. Put newitem there, and bubble it up
# to its final resting place (by sifting its parents down).
heap[pos] = newitem
_siftdown(heap, startpos, pos)
def _siftdown(heap, startpos, pos):
newitem = heap[pos]
# Follow the path to the root, moving parents down until finding a place
# newitem fits.
while pos > startpos:
parentpos = (pos - 1) >> 1
parent = heap[parentpos]
if newitem < parent:
heap[pos] = parent
pos = parentpos
continue
break
heap[pos] = newitem
heapq.heappop底层代码实现
def heappop(heap):
"""Pop the smallest item off the heap, maintaining the heap invariant."""
lastelt = heap.pop() # raises appropriate IndexError if heap is empty
if heap:
returnitem = heap[0]
heap[0] = lastelt
_siftup(heap, 0)
return returnitem
return lastelt
python堆模块_python内置堆模块相关推荐
- python中的random模块_Python内置random模块生成随机数的方法
本文我们详细地介绍下两个模块关于生成随机序列的其他使用方法. 随机数参与的应用场景大家一定不会陌生,比如密码加盐时会在原密码上关联一串随机数,蒙特卡洛算法会通过随机数采样等等.Python内置的ran ...
- python生成50个随机数_Python内置random模块生成随机数的方法
本文我们详细地介绍下两个模块关于生成随机序列的其他使用方法. 随机数参与的应用场景大家一定不会陌生,比如密码加盐时会在原密码上关联一串随机数,蒙特卡洛算法会通过随机数采样等等.Python内置的ran ...
- python自带笔记本电脑_Python内置常用模块
time和datatime Range os sys hashlib XML json & picle 1.time和datetime time和datetime都是python处理时间和日期 ...
- python compile函数_python 内置函数 compile()
python 内置函数 compile() 这个函数用来编译一段字符串的源码,结果可以生成字节码或者AST(抽像语法树),字节码可以使用函数exec()来执行,而AST可以使用eval()来继续编译. ...
- python函数整理_python内置函数整理
为了方便记忆,将这些内置函数进行了如下分类:数学运算(7个) 类型转换(24个) 序列操作(8个) 对象操作(7个) 反射操作(8个) 变量操作(2个) 交互操作(2个) 文件操作(1个) 编译执行( ...
- python property函数_Python内置函数property()如何使用
代码 class Shuxing(): def __init__(self, size = 10): self.size = size def getSize(self): print('getSiz ...
- python排序方法_python内置的排序方法
Python 内置的 sort() 方法可以实现对列表的原地排序功能.内置的 sorted() 函数则不会修改原列表,而是生成一个经过排序的新列表. 下面总结一些常用的排序方法. 基本排序 最简单的方 ...
- python filter函数_python 内置函数filter
python 内置函数filter class filter(object): """ filter(function or None, iterable) --> ...
- python long函数_python 内置函数
查看内置函数: >>> dir(__builtins__) min(arg1, arg2, *args, *[, key=func])函数:函数至少传入两个参数,但是有只传入一个参数 ...
最新文章
- 我在兰亭这三年完结篇之离开
- 更改Apache默认网站根目录
- model.fit() 参数详解【TensorFlow2入门手册】
- 模板类的定义和实现可以分开吗?
- Redis系列教程(四):Redis为什么是单线程、及高并发快的3大原因详解
- SpringBoot使用ELK日志收集
- strspn和strcspn妙用
- 如何使用Movavi Video Editor制作幻灯片?
- 惠普计算机图标不在桌面,惠普笔记本电脑桌面图标显示图标不正常如何还原 惠普笔记本电脑桌面图标显示图标不正常还原的方法...
- java home websphere_websphere6.1安装与配置
- Whai_springmvc笔记
- 时间(空间)复杂度 O(N) 的理解
- 每日MySQL之010:导出Delimited-Text格式文件
- 【CKA考试笔记】十三、k8s中的网络
- 1998年图灵奖--詹姆斯·格雷简介
- 读书笔记:redis开发与运维 基础篇
- 精讲精练_参悟Android核心技术-张晓飞-专题视频课程
- [Untiy]贪吃蛇大作战(一)——开始界面
- 【删除含有合并单元格的excel某列】
- PAT乙级题目合集(思路笔记)
热门文章
- ie浏览器ip代理怎么设置
- 关于tomcat启动失败的一个原因
- 自定义spring配置文件位置
- java togglebutton_ToggleButton和Switch使用大全
- linux my.cnf基本参数,Linux中MySQL配置文件my.cnf参数说明
- python 心电处理包_python 黏包现象及其解决方案
- 在java中null的作用_在java中避免使用!= null有什么好处?
- 10067mysql_MYSQL数据库mysql Non-Transactional Database Only(只支持MyISAM)
- dijkstra算法代码_深度好文:改变了我们生活方式最有影响力的5种图算法
- Shiro————会话管理