一 :集合库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 模块使用说明相关推荐

  1. python之堆heapq模块

    python之堆heapq模块 堆是一种特殊的树形结构,通常我们所说的堆的数据结构指的是完全二叉树,并且根节点的值小于等于该节点所有子节点的值. 堆是非线性的树形的数据结构,有两种堆,最大堆与最小堆. ...

  2. Python 中 concurrent.futures 模块使用说明

    Python 中 concurrent.futures 模块使用说明 转载请注明出处:https://blog.csdn.net/jpch89/article/details/87643972 文章目 ...

  3. python基础之heapq模块(堆模块)

    堆是一种数据结构,本质上是一种二叉树.在python中可以使用heapq模块实现,heapq可以实现一个简单的优先级队列. 一.堆的性质 堆分为小根堆与大根堆,小根堆的第一个元素可以理解为数值最小的元 ...

  4. Python取top N相关的模块:heapq模块

    Python取top N相关的模块:heapq模块 原文:[Python]Python取top N相关的模块:heapq模块_小怪孩的成长之路-CSDN博客_python 取top 最近在程序中需要取 ...

  5. python heapq模块使用

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

  6. python树状节点 可拖拽_Python 的 heapq 模块源码分析

    原文链接:Python 的 heapq 模块源码分析 起步 heapq 模块实现了适用于Python列表的最小堆排序算法. 堆是一个树状的数据结构,其中的子节点都与父母排序顺序关系.因为堆排序中的树是 ...

  7. Python 的 heapq 模块源码分析

    作者:weapon 来源:https://zhuanlan.zhihu.com/p/54260935 起步 heapq 模块实现了适用于Python列表的最小堆排序算法. 堆是一个树状的数据结构,其中 ...

  8. Python中的堆实现:heapq 模块——利用堆结构实现快速访问数据流中的中位数

    堆结构 堆结构是一种优先队列,可以以任意顺序添加对象,并随时查找或删除最小(大)的元素,或者查找和删除前 K 个最小(大)元素.相比于列表方法min() / max(),这样做的效率要高得多. 堆结构 ...

  9. python分配 使最大的最小_python3中的heapq模块使用

    ###heapq-堆排序算法 heapq实现了一个适合与Python的列表一起使用的最小堆排序算法. 二叉树 树中每个节点至多有两个子节点 满二叉树 树中除了叶子节点,每个节点都有两个子节点 什么是完 ...

最新文章

  1. JavaのFile类基础操作之问题
  2. android某个活动全屏,android – 重新组合活动布局,以便在旋转屏幕时全屏播放视频...
  3. 【FPGA】单端口RAM的设计(异步读、同步写)
  4. $$和$BASHPID区别
  5. 面试 | 蚂蚁金服面试经历
  6. Dubbo服务引入原理
  7. 用自定义的form表单对jqgrid数据进行检索查询
  8. 比较jquery中的after(),append(),appendTo()方法
  9. python安装mysql模块_Python:使用pip安装MySQL-python模块
  10. 除了停电之外,今年的CES还有这些“意外”……
  11. 4.测试用例模板(p2p)
  12. 解决win10cmd乱码
  13. 怎么定位前后端问题之-图片显示不出来显示空白等
  14. learn python the hard way中文pdf_Learn Python 3 the Hard Way .pdf
  15. python123货币转换器_Python货币换算I,Python123python,转换
  16. 【汇编语言实验一】用机器指令和汇编指令编程(1)
  17. 卸载wps后office图标丢失变白,系统卡顿
  18. 彻底关闭自带杀毒软件windows defender,Antimalware Service Executable
  19. leetcode189 python旋转数组
  20. Xcode全局搜索搜不到部分内容解决

热门文章

  1. 地推HTTP3和QUIC
  2. 人到六十岁还是黑头发好吗?
  3. 普通人如何走上复利投资致富的门路?
  4. 你有过什么令你难忘的约会经历?
  5. 去越南旅游一个人玩一个月需要多少人民币?
  6. 专升本的学生和高考考上本科的学生一样吗
  7. 玩抖音,你喜欢的,都是对自身没好处的
  8. 电商并没有给我们创造一个就业机会
  9. 年轻时听到别人金钱至上的话语
  10. 垄断者 Google Chrome 若被迫分拆,谁将会接盘?