python 内置模块:collections
设置坐标: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相关推荐
- python内置模块_三分钟读懂Python内置模块collections
collections模块 Python内置模块,在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter.deque.d ...
- python内置模块和内置方法
python内置模块 time json re logging random os sys xml pikle shelve getopt uuid subprocess collections fu ...
- python turtle循环图案-Python内置模块turtle绘图详解
urtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x.纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行的 ...
- 【万字长文详解】Python库collections,让你击败99%的Pythoner
Python的collections库实现了特定目标的容器,以提供Python标准内建容器 dict , list , set , 和 tuple 的替代选择. 为很多用其他方法很难实现的场景提供了解 ...
- 每天学点Python之collections
每天学点Python之collections 内容摘抄自:<python大法好>的每天学点Python之collections collections模块在内置数据类型(dict.list ...
- python:collections模块
Counter类 介绍:A counter tool is provided to support convenient and rapid tallies 构造:class collections. ...
- Python中collections模块
目录 Python中collections模块:模块实现了特定目标的容器,以提供Python标准内建容器 dict.list.set.tuple 的替代选择. Counter:字典的子类,提供了可哈希 ...
- Python中Collections模块的Counter容器类使用教程
Python中Collections模块的Counter容器类使用教程 1.collections模块 collections模块自Python2.4版本开始被引入,包含了dict,set,list, ...
- Python的collections模块中namedtuple结构使用示例
namedtuple顾名思义,就是名字+元组的数据结构,下面就来看一下Python的collections模块中namedtuple结构使用示例 namedtuple 就是命名的 tuple,比较像 ...
- python config模块_用Python内置模块处理ini配置文件
原标题:用Python内置模块处理ini配置文件 简介 开发人员每天都在处理一些大型而复杂的项目, 而配置文件会帮到我们并节省不少时间.在处理配置文件过程中,无需更改源代码本身,只需要调整配置文件即可 ...
最新文章
- R语言ggplot2可视化散点图、并使用geom_encircle函数自定义多边形圈定可视化图像中的指定区域、使用geom_smooth函数基于loess方法拟合数据点之间的趋势关系曲线
- 图解Oracle存储过程教程
- access 导入 txt sql语句_从零开始学习 MySQL 系列索引、视图、导入和导出
- java中String值为空字符串与null的判断方法
- Spring源码:AOP转文
- 什么是大端序和小端序,为什么要有字节序
- 数码摄影入门之二 准确的曝光
- 传网易云音乐高管变动:市场副总裁李茵离职 CEO被降权
- VUE颜色选择器插件vColorPicker
- vim修改tab默认4个空格
- CSS知识点梳理(二)
- 73种网页常用js代码
- 格雷码(Gray Code)编码、实现及规律(附编码)
- Windows中I/O完成端口机制详解
- 996工作制,还要抽时间提升自己吗?
- oracle 返回部分行,求救,返回部分结果后,才报单行子查询返回多行。
- 阶段性PJ某软件APK
- 避免当野monkey, 走野路子
- Cannot Resolve Symbol xxx 错误解决
- 关于webpack5打包图片不报错但是也不显示问题