一 :集合库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 模块使用说明的更多相关文章

  1. python中的threading模块使用说明

    这段时间使用python做串口的底层库,用到了多线程,对这部分做一下总结.实际用完了后再回过头去看python的官方帮助文档,感觉受益匪浅,把里面的自己觉得有用的一些关键点翻译出来,留待后续查验.th ...

  2. python标准库:collections和heapq模块

    http://blog.csdn.net/pipisorry/article/details/46947833 python额外的数据类型.collections模块和heapq模块的主要内容. 集合 ...

  3. Python heapq 模块的实现 - A Geek's Page

    Python heapq 模块的实现 - A Geek's Page Python heapq 模块的实现

  4. Python常用数据结构之heapq模块

    Python数据结构常用模块:collections.heapq.operator.itertools heapq 堆是一种特殊的树形结构,通常我们所说的堆的数据结构指的是完全二叉树,并且根节点的值小 ...

  5. python heapq模块使用

    Python内置的heapq模块 Python3.4版本中heapq包含了几个有用的方法: heapq.heappush(heap,item):将item,推入heap >>> it ...

  6. Python heapq模块

    注意,默认的heap是一个小顶堆! heapq模块提供了如下几个函数: heapq.heappush(heap, item) 把item添加到heap中(heap是一个列表) heapq.heappo ...

  7. python 中的堆 (heapq 模块)应用:Merge K Sorted Lists

    堆是计算机科学中一类特殊的数据结构的统称.堆通常是一个可以被看做一棵树的数组对象.在队列中,调度程序反复提取队列中第一个作业并运行,因为实际情况中某些时间较短的任务将等待很长时间才能结束,或者某些不短 ...

  8. python 关于heapq模块的随笔

    heapq模块提供了很多高级功能可以通过help(heapq)查看详细文档: 要点: 1优先级队列让我们可以按照重要程度来处理元素,而不是先进先出 2使用heapq可以应对长列表,因为heap不是复杂 ...

  9. python的urllib2库详细使用说明

    一直以来技术群里会有新入行的同学提问关于urllib和urllib2以及cookielib相关的问题.所以我打算在这里总结一下,避免大家反复回答同样的问题浪费资源. 这篇属于教程类的文字,如果你已经非 ...

随机推荐

  1. java中调用数据库中的存储过程和函数

    public static void main(String[] args)    {         Connection conn =getConnection(url,user, pwd);   ...

  2. sourceInsight设置

    复制后光标定格在行尾 Options -> Preferences -> Typing -> After pasting,select:at end of pasted text t ...

  3. django的html模板中获取字典的值

    在django的html模板中获取字典中的值应当直接使用 字典.[key] 的方式 {% for i in lists %} <li id="{{i.id}}" class ...

  4. vue 过渡状态

    vue的过渡系统提供了非常多简单的方法设置进入.离开和列表的动效.那么对于数据元素本身的动效呢,例: 数字和运算 颜色的显示 svg节点的位置 元素的大小和其他的属性 所有的原始数字都被事先存储起来, ...

  5. linux c log 日志接口

    #define SIZE_16M 16777216             //1024*1024*16 #define LOG_FILE_PATH "./mylog.txt"   ...

  6. IPC之SystemV

    svipc - System V interprocess communication mechanisms linux实现的System V interprocess communication ( ...

  7. GPU 属性

    struct cudaDeviceProp { ]; /**< 设备的ASCII标识 */ size_t totalGlobalMem; /**< 可用的全局内存量,单位字节 */ siz ...

  8. 宝宝书 & 网站

    1. 网站 妈妈帮 宝宝树 2. 图书推荐 育儿百科 育儿经 中国儿童智力方程 聪明宝宝营养食谱1001例

  9. jsp时间格式化

    <fmt:formatDate value="${start.time }" pattern="HH:mm" />

  10. 我的第十个java程序--(其实是修改别人的web代码{springmvc+mybatis},知道了原理后其实一切都变的很简单)

    先是效果图,提高学习的兴趣 springmvc+mybatis+easyui 这几个东西让我一点一点的啃,刚开始的时候真的跑不起几个程序 1.下载tomcat 2.导入程序 3.项目报错,但不指出具体 ...