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]
python collection 和 heapq 模块使用说明的更多相关文章
- python中的threading模块使用说明
这段时间使用python做串口的底层库,用到了多线程,对这部分做一下总结.实际用完了后再回过头去看python的官方帮助文档,感觉受益匪浅,把里面的自己觉得有用的一些关键点翻译出来,留待后续查验.th ...
- python标准库:collections和heapq模块
http://blog.csdn.net/pipisorry/article/details/46947833 python额外的数据类型.collections模块和heapq模块的主要内容. 集合 ...
- Python heapq 模块的实现 - A Geek's Page
Python heapq 模块的实现 - A Geek's Page Python heapq 模块的实现
- Python常用数据结构之heapq模块
Python数据结构常用模块:collections.heapq.operator.itertools heapq 堆是一种特殊的树形结构,通常我们所说的堆的数据结构指的是完全二叉树,并且根节点的值小 ...
- python heapq模块使用
Python内置的heapq模块 Python3.4版本中heapq包含了几个有用的方法: heapq.heappush(heap,item):将item,推入heap >>> it ...
- Python heapq模块
注意,默认的heap是一个小顶堆! heapq模块提供了如下几个函数: heapq.heappush(heap, item) 把item添加到heap中(heap是一个列表) heapq.heappo ...
- python 中的堆 (heapq 模块)应用:Merge K Sorted Lists
堆是计算机科学中一类特殊的数据结构的统称.堆通常是一个可以被看做一棵树的数组对象.在队列中,调度程序反复提取队列中第一个作业并运行,因为实际情况中某些时间较短的任务将等待很长时间才能结束,或者某些不短 ...
- python 关于heapq模块的随笔
heapq模块提供了很多高级功能可以通过help(heapq)查看详细文档: 要点: 1优先级队列让我们可以按照重要程度来处理元素,而不是先进先出 2使用heapq可以应对长列表,因为heap不是复杂 ...
- python的urllib2库详细使用说明
一直以来技术群里会有新入行的同学提问关于urllib和urllib2以及cookielib相关的问题.所以我打算在这里总结一下,避免大家反复回答同样的问题浪费资源. 这篇属于教程类的文字,如果你已经非 ...
随机推荐
- 从Python的角度来看编码与解码
导语: Python2和Python3中,因为默认字符集的不同而造成的麻烦,简直是程序员的梦魇!要彻底告别这个麻烦,就需要从本质上来理解编码和解码. 为什么要有编码? 对于不会英文的中国人来说,将英文 ...
- docker jupyter
151 curl -sSL https://get.docker.com/|sh 152 sudo usermode -aG docker ubuntu 153 sudo usermod -aG ...
- The Definitive Guide To Django 2 学习笔记(二) 第二个View 动态内容
“Hello World”只是简单的展现了Django 基本的工作机制,但它不是动态的网页.第二个View我们将创建一个动态的网页,该页面上将展现当前的时间和日期. 该View需要做两件事,第一,计算 ...
- Dubbo—Zookeeper的典型应用
1.Zookeeper 作为 Hadoop 项目中的一个子项目,是 Hadoop 集群管理的一个必不可少的模块,它主要用来控制集群中的数据,如它管理 Hadoop 集群中的 NameNode,还有 H ...
- jQuery-对Select的操作集合[终结篇]
jQuery获取Select选择的Text和Value: 请选择 C# Javascript jQuery C++ Java VB 选择一项试试看语法解释:1. $("#select_id ...
- Ubuntu安装qBittorrent
qBitTorrent是Ubuntu Linux中最受欢迎的P2P软件之中的一个. 出自一名法国大学生之手的qBitTorrent功能强大.界面精美.操作直观. qBitTorrent是Linux中最 ...
- ios APP 在 waiting for review时重新上传程序
今天上传程序后发现了一个挺大的bug,想重新上传app,于是搜了一下,现记录一下: 点击details进入后在link点击binary details,进入之后点击右上角reject this bin ...
- 《转》适用于开发人员的10个最佳ASP.NET的CMS系统
1) mojoportal mojoPortal 是一个开源的.用 C# 编写的站点框架和内容管理系统,可以运行在 Windows 中的 ASP.NET 和 Linux/Mac OS X 中的 Mon ...
- pycharm 相关设置问题
pycharm设置自动换行 file→settings→Editor→General→勾选 Use soft wraps in eitor → ok
- 涨姿势UWP源码——IsolatedStorage
前一篇涨姿势UWP源码分析从数据源着手,解释了RSS feed的获取和解析,本篇则会就数据源的保存和读取进行举例. 和之前的Windows Runtime一样,UWP采用IsolatedStorage ...