设置坐标:namedtuple

格式:

变量名 = namedtuple(任意名,list)

from collections import namedtuplePoint = namedtuple('point', ['x', 'y', 'z'])
p = Point(1,2,0)
print(p.x)
print(p.y, p.z)print(isinstance(p, tuple))

运行结果:

1
2 0
True

双向队列:deque

https://docs.python.org/2.7/library/collections.html?highlight=deque#collections.deque

from collections import deque
q = deque(['a', 'b', 'c'])
q.append('x')       #尾插
q.appendleft('y')   #头插
print(q)

运行结果:

deque(['y', 'a', 'b', 'c', 'x'])

默认字典:defaultdict, 如果没有该key值,默认输出指定值。

from collections import defaultdict
dd = defaultdict(lambda: 'N/A')
dd['key1'] = 'abc'
print (dd['key1']) # key1存在
print (dd['key2'])

运行结果:

abc
N/A

顺序排列dist: OrderedDict

from collections import OrderedDict
d = dict([('a', 1), ('b', 2), ('c', 3)])
n = {'a':1, 'b':2, 'c':3}
print (d) # dict的Key是无序的
print (n) # dict的Key是无序的od = OrderedDict([('a', 1), ('b', 2), ('c', 3)])
print (od)# OrderedDict的Key是有序的

运行结果:

{'c': 3, 'b': 2, 'a': 1}
{'c': 3, 'b': 2, 'a': 1}
OrderedDict([('a', 1), ('b', 2), ('c', 3)])

计算数目:Counter

from collections import Counterc = Counter()
for ch in 'programming':c[ch] = c[ch] + 1print(c)
print(c)

运行结果:

Counter({'p': 1})
Counter({'r': 1, 'p': 1})
Counter({'r': 1, 'o': 1, 'p': 1})
Counter({'r': 1, 'o': 1, 'p': 1, 'g': 1})
Counter({'r': 2, 'o': 1, 'p': 1, 'g': 1})
Counter({'r': 2, 'o': 1, 'p': 1, 'g': 1, 'a': 1})
Counter({'r': 2, 'o': 1, 'p': 1, 'g': 1, 'a': 1, 'm': 1})
Counter({'r': 2, 'm': 2, 'o': 1, 'p': 1, 'g': 1, 'a': 1})
Counter({'r': 2, 'm': 2, 'o': 1, 'i': 1, 'p': 1, 'g': 1, 'a': 1})
Counter({'r': 2, 'm': 2, 'o': 1, 'i': 1, 'n': 1, 'p': 1, 'g': 1, 'a': 1})
Counter({'r': 2, 'g': 2, 'm': 2, 'o': 1, 'i': 1, 'n': 1, 'p': 1, 'a': 1})
Counter({'r': 2, 'g': 2, 'm': 2, 'o': 1, 'i': 1, 'n': 1, 'p': 1, 'a': 1})

转载于:https://blog.51cto.com/13502993/2151744

python 内置模块:collections相关推荐

  1. python内置模块_三分钟读懂Python内置模块collections

    collections模块 Python内置模块,在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter.deque.d ...

  2. python内置模块和内置方法

    python内置模块 time json re logging random os sys xml pikle shelve getopt uuid subprocess collections fu ...

  3. python turtle循环图案-Python内置模块turtle绘图详解

    urtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x.纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行的 ...

  4. 【万字长文详解】Python库collections,让你击败99%的Pythoner

    Python的collections库实现了特定目标的容器,以提供Python标准内建容器 dict , list , set , 和 tuple 的替代选择. 为很多用其他方法很难实现的场景提供了解 ...

  5. 每天学点Python之collections

    每天学点Python之collections 内容摘抄自:<python大法好>的每天学点Python之collections collections模块在内置数据类型(dict.list ...

  6. python:collections模块

    Counter类 介绍:A counter tool is provided to support convenient and rapid tallies 构造:class collections. ...

  7. Python中collections模块

    目录 Python中collections模块:模块实现了特定目标的容器,以提供Python标准内建容器 dict.list.set.tuple 的替代选择. Counter:字典的子类,提供了可哈希 ...

  8. Python中Collections模块的Counter容器类使用教程

    Python中Collections模块的Counter容器类使用教程 1.collections模块 collections模块自Python2.4版本开始被引入,包含了dict,set,list, ...

  9. Python的collections模块中namedtuple结构使用示例

    namedtuple顾名思义,就是名字+元组的数据结构,下面就来看一下Python的collections模块中namedtuple结构使用示例 namedtuple 就是命名的 tuple,比较像 ...

  10. python config模块_用Python内置模块处理ini配置文件

    原标题:用Python内置模块处理ini配置文件 简介 开发人员每天都在处理一些大型而复杂的项目, 而配置文件会帮到我们并节省不少时间.在处理配置文件过程中,无需更改源代码本身,只需要调整配置文件即可 ...

最新文章

  1. 20160531-20160607springmvc入门
  2. java面试总结(第一天)
  3. 最大素数c语言,for语句计算输出10000以内最大素数怎么搞最简单??各位大神们...
  4. windows安装dcm4chee 出错 check file system group LOSSY_STORAGE for deletion
  5. 利用cli.go来写命令行应用
  6. enableEventValidation是干什么的?
  7. LeetCode动态规划系列教程(上)
  8. lammps后处理:Python调用Ovito模块配置方法
  9. ECS用户一定要看到最后有惊喜
  10. php变量控制结构与函数,LAMP兄弟连原创视频教程(PHP笔记一--变量,流程控制结构,函数)...
  11. win10系统怎么找服务器地址,win10系统下如何快速查找本地ip地址
  12. 2021年安徽省大数据与人工智能应用竞赛大数据-本科组赛题
  13. autoit java_AutoIt3客户端和Java服务器端TCP通信
  14. 【没有刀剑,如何行走江湖】半晌私语(上)
  15. 利用NCBIdatasets批量下载大规模生信数据集
  16. 桌面上打开计算机有延迟感觉,电脑中右击操作反应慢如何解决|解决右键菜单弹出延迟的方法...
  17. 重庆轻工职业学院计算机期末考试,重庆轻工职业学院教务网络管理系统 http://183.230.5.161:8082,精英高考网...
  18. Android Studio 实现将视频资源嵌入APP中
  19. 人之间的尊重是相互的_人与人之间彼此尊重是相互的,你若敬我一尺,我必敬你一丈...
  20. ZOJ 3797 Sister's Noise 组合+DP

热门文章

  1. html5怎么实现自动缩放图片,如图,html5开发的手机端web在线客服聊天,如何实现图片点击放大,捏合缩放功能?...
  2. 【HDU4507】恨7不成妻
  3. This text field does not specify an inputType or a hint
  4. CCNP-2 EIGRP试验2(BSCI)
  5. tomcat 修改默认字符集
  6. [K/3Cloud] KSQL 关联表更新字段Update语法
  7. node 同步js代码-超越昨天的自己系列(5)
  8. iphone开发如何测试?
  9. Ubuntu中的默认shell
  10. ADSL路由器防止******