python collection 和 heapq 模块使用说明
一 :集合库collection
python 拥有一些内置的数据类型,collections模块提供啦几个额外的数据类型:
1,namedtuple 生成可以使用名字来访问元素内容的tuple子类
2,deque 双端队列,可以加速从另一侧追加和推出对象
3,counter 计数器,主要用来计数
4,orderedDict 有序字典
5,defaultdict 带有默认值的字典
1) namedtuple 命名的元祖形式,一般需要知道元祖里面每个字段代表什么含义,可以用命名元祖namedtuple
继承命名的tuples
2)deque 双端队列
deque最大的好处就是实现了从队列 头部快速增加和取出对象,比如popleft() 和appendleft()
append() appendleft() pop() popleft() extend() extendleft() rotate()
1,使用最多的就是限制队列长度,来获取队列的最后一个值或者几个值
2) deque 的其他用法,可以参考下
3) Counter 计数器
elements 返回一个迭代器,显示重复次数的元素,如果次数小于1,则被忽略
most_common 获取出现次数最多的元素
subtract 两个元素counter的元素进行相减
4) ordereddict 有序字典
5)defaultdict 默认字典
二 : heapd 堆队列
- heapq.heappush(heap, item)
-
Push the value item onto the heap, maintaining the heap invariant.
- heapq.heappop(heap)
-
Pop and return the smallest item from the heap, maintaining the heapinvariant. If the heap is empty,IndexError is raised. To access thesmallest item without popping it, use heap[0].
- heapq.heappushpop(heap, item)
-
Push item on the heap, then pop and return the smallest item from the heap. The combined action runs more efficiently than heappush()followed by a separate call to heappop().
- heapq.nlargest(n, iterable, key=None)
-
Return a list with the n largest elements from the dataset defined byiterable. key, if provided, specifies a function of one argument that isused to extract a comparison key from each element in the iterable:key=str.lower Equivalent to: sorted(iterable, key=key,reverse=True)[:n]
- heapq.nsmallest(n, iterable, key=None)
-
Return a list with the n smallest elements from the dataset defined byiterable. key, if provided, specifies a function of one argument that isused to extract a comparison key from each element in the iterable:key=str.lower Equivalent to: sorted(iterable, key=key)[:n]
转载于:https://www.cnblogs.com/1204guo/p/8520887.html
python collection 和 heapq 模块使用说明相关推荐
- python之堆heapq模块
python之堆heapq模块 堆是一种特殊的树形结构,通常我们所说的堆的数据结构指的是完全二叉树,并且根节点的值小于等于该节点所有子节点的值. 堆是非线性的树形的数据结构,有两种堆,最大堆与最小堆. ...
- Python 中 concurrent.futures 模块使用说明
Python 中 concurrent.futures 模块使用说明 转载请注明出处:https://blog.csdn.net/jpch89/article/details/87643972 文章目 ...
- python基础之heapq模块(堆模块)
堆是一种数据结构,本质上是一种二叉树.在python中可以使用heapq模块实现,heapq可以实现一个简单的优先级队列. 一.堆的性质 堆分为小根堆与大根堆,小根堆的第一个元素可以理解为数值最小的元 ...
- Python取top N相关的模块:heapq模块
Python取top N相关的模块:heapq模块 原文:[Python]Python取top N相关的模块:heapq模块_小怪孩的成长之路-CSDN博客_python 取top 最近在程序中需要取 ...
- python heapq模块使用
Python内置的heapq模块 Python3.4版本中heapq包含了几个有用的方法: heapq.heappush(heap,item):将item,推入heap >>> it ...
- python树状节点 可拖拽_Python 的 heapq 模块源码分析
原文链接:Python 的 heapq 模块源码分析 起步 heapq 模块实现了适用于Python列表的最小堆排序算法. 堆是一个树状的数据结构,其中的子节点都与父母排序顺序关系.因为堆排序中的树是 ...
- Python 的 heapq 模块源码分析
作者:weapon 来源:https://zhuanlan.zhihu.com/p/54260935 起步 heapq 模块实现了适用于Python列表的最小堆排序算法. 堆是一个树状的数据结构,其中 ...
- Python中的堆实现:heapq 模块——利用堆结构实现快速访问数据流中的中位数
堆结构 堆结构是一种优先队列,可以以任意顺序添加对象,并随时查找或删除最小(大)的元素,或者查找和删除前 K 个最小(大)元素.相比于列表方法min() / max(),这样做的效率要高得多. 堆结构 ...
- python分配 使最大的最小_python3中的heapq模块使用
###heapq-堆排序算法 heapq实现了一个适合与Python的列表一起使用的最小堆排序算法. 二叉树 树中每个节点至多有两个子节点 满二叉树 树中除了叶子节点,每个节点都有两个子节点 什么是完 ...
最新文章
- JavaのFile类基础操作之问题
- android某个活动全屏,android – 重新组合活动布局,以便在旋转屏幕时全屏播放视频...
- 【FPGA】单端口RAM的设计(异步读、同步写)
- $$和$BASHPID区别
- 面试 | 蚂蚁金服面试经历
- Dubbo服务引入原理
- 用自定义的form表单对jqgrid数据进行检索查询
- 比较jquery中的after(),append(),appendTo()方法
- python安装mysql模块_Python:使用pip安装MySQL-python模块
- 除了停电之外,今年的CES还有这些“意外”……
- 4.测试用例模板(p2p)
- 解决win10cmd乱码
- 怎么定位前后端问题之-图片显示不出来显示空白等
- learn python the hard way中文pdf_Learn Python 3 the Hard Way .pdf
- python123货币转换器_Python货币换算I,Python123python,转换
- 【汇编语言实验一】用机器指令和汇编指令编程(1)
- 卸载wps后office图标丢失变白,系统卡顿
- 彻底关闭自带杀毒软件windows defender,Antimalware Service Executable
- leetcode189 python旋转数组
- Xcode全局搜索搜不到部分内容解决