设置坐标: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. R语言ggplot2可视化散点图、并使用geom_encircle函数自定义多边形圈定可视化图像中的指定区域、使用geom_smooth函数基于loess方法拟合数据点之间的趋势关系曲线
  2. 图解Oracle存储过程教程
  3. access 导入 txt sql语句_从零开始学习 MySQL 系列索引、视图、导入和导出
  4. java中String值为空字符串与null的判断方法
  5. Spring源码:AOP转文
  6. 什么是大端序和小端序,为什么要有字节序
  7. 数码摄影入门之二 准确的曝光
  8. 传网易云音乐高管变动:市场副总裁李茵离职 CEO被降权
  9. VUE颜色选择器插件vColorPicker
  10. vim修改tab默认4个空格
  11. CSS知识点梳理(二)
  12. 73种网页常用js代码
  13. 格雷码(Gray Code)编码、实现及规律(附编码)
  14. Windows中I/O完成端口机制详解
  15. 996工作制,还要抽时间提升自己吗?
  16. oracle 返回部分行,求救,返回部分结果后,才报单行子查询返回多行。
  17. 阶段性PJ某软件APK
  18. 避免当野monkey, 走野路子
  19. Cannot Resolve Symbol xxx 错误解决
  20. 关于webpack5打包图片不报错但是也不显示问题

热门文章

  1. git切换到指定tag
  2. openNebula 搭建
  3. 转】Spark DataFrame小试牛刀
  4. Nginx Location配置总结
  5. 开发环境搭建(1)——JDK安装
  6. linux系统的日常管理2
  7. Windows mgmt command commom
  8. Resin服务器平台介绍
  9. typedef VS #define —— C语言中的 关键字 与 C指令
  10. 右左法则----复杂指针解析