Python模块:collections
collections模块常用的数据类型:
(1)namedtuple(): #创建一个自定义的tuple对象,并且规定了tuple元素的个数,并可以用属性而不是索引来引用tuple的某个元素。 from collections import namedtuple
p = namedtuple('p',['x','y','z'])
p1 = p(2,3,5)
print(p1.x,p1.y,p1.z) 输出结果为:
2 3 5
(2)deque(): #使用list存储数据时,按索引访问元素很快。由于list是线性存储,数据量大的时候,插入和删除元素效率就很低。deque队列是为了高效的实现插入和删除操作的双向列表,适用于队列和栈。 from collections import deque li1 = deque([1,2,3,4,5])
li1.append('a')
li1.appendleft('m')
print(li1) li1.pop()
li1.popleft()
print(li1) 输出结果为:
deque(['m', 1, 2, 3, 4, 5, 'a'])
deque([1, 2, 3, 4, 5])
(3)defaultdict(): #使用字典的时候,如果key值不存在就会报KeyError错。如果想key不存在时,返回一个默认值,就可以用defaultdict: from collections import defaultdict dd = defaultdict(lambda :'not exist!')
dd['k1'] = ''
print(dd['k1'])
print(dd['k2']) 输出结果为:
123
not exist!
(4)有序字典
from collections import OrderedDict
dd = dict([('k1',1),('k2',2),('k3',3)])
print(dd)
order_dd =OrderedDict([('k1',1),('k2',2),('k3',3)])
print(order_dd)
输出结果为:
{'k3': 3, 'k2': 2, 'k1': 1}
OrderedDict([('k1', 1), ('k2', 2), ('k3', 3)])
(5)Counter(): #计数器,统计字符串里面所有元素出现次数。 from collections import Counter
s = "A Counter is a dict subclass for counting hashable objects. It is an unordered collection where elements are stored as dictionary keys and their counts are stored as dictionary values. " c = Counter(s)
print(c.most_common(5)) #统计出现次数最多的5个元素 输出结果为:
[(' ', 30), ('e', 18), ('s', 15), ('a', 13), ('t', 13)]
Python模块:collections的更多相关文章
- python模块--collections
python的内建模块collections有几个关键的数据结构,平常在使用的时候,开发者可以直接调用,不需要自己重复制造轮子,这样可以提高开发效率. 1. deque双端队列 平常我们使用的pyth ...
- python模块collections中namedtuple()的理解
Python中存储系列数据,比较常见的数据类型有list,除此之外,还有tuple数据类型.相比与list,tuple中的元素不可修改,在映射中可以当键使用.tuple元组的item只能通过index ...
- Python 模块collections
1.深入理解python中的tuple的功能 基本特性 # 可迭代 name_tuple = ('0bug', '1bug', '2bug') for name in name_tuple: prin ...
- 不可不知的Python模块: collections
原文:http://www.zlovezl.cn/articles/collections-in-python/ Python作为一个“内置电池”的编程语言,标准库里面拥有非常多好用的模块.比如今天想 ...
- python模块--collections(容器数据类型)
Counter类(dict的子类, 计数器) 方法 返回值类型 说明 __init__ Counter 传入可迭代对象, 会对对象中的值进行计数, 值为键, 计数为值 .elements() 迭代器 ...
- Python中collections模块
目录 Python中collections模块 Counter defaultdict OrderedDict namedtuple deque ChainMap Python中collections ...
- Python之常用模块--collections模块
认识模块 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编写的 ...
- 【转】python模块分析之collections(六)
[转]python模块分析之collections(六) collections是Python内建的一个集合模块,提供了许多有用的集合类. 系列文章 python模块分析之random(一) pyth ...
- Python的collections模块中namedtuple结构使用示例
namedtuple顾名思义,就是名字+元组的数据结构,下面就来看一下Python的collections模块中namedtuple结构使用示例 namedtuple 就是命名的 tuple,比较 ...
- python常用模块collections os random sys
Python 模块(Module),是一个 Python 文件,以 .py 结尾,包含了 Python 对象定义和Python语句. 模块让你能够有逻辑地组织你的 Python 代码段. 把相关的代码 ...
随机推荐
- 许小年:宁可踏空,不可断粮<转>
http://www.daonong.com/g/25/xsqy/2014/0716/51074.html 文│许小年 中欧国际工商学院教授 为什么我们企业的创新能力长期处于低水平呢? 深入观察,内心 ...
- maven 项目出现 java.lang.ClassNotFoundException
需要修改的有两个地方1.项目根目录下的.project文件,用记事本打开,加入以下代码(把原来的<buildSpec>节点和<natures>替换了): <buildSp ...
- 施耐德Sepam 40系列备自投逻辑
1# 主供: VL1= NOT PVTS_1_3 V1 = VL1 AND P59_1_7 AND P59_1_8 AND P59_1_9VL2 = VL1 AND I12 AND I21 AND I ...
- 重置zend studio 默认设置的方法
转载自:http://www.zendstudio.net/archives/reset-the-zend-studio-settings/ 这个方法类似于手机的"恢复出厂设置"的 ...
- Foundation ----->NSNumber
/*--------------------NSNumber--------------------*/ //包装基本数据类型 //1.创建number对象 //12 ...
- html focus事件小学问
focus事件千万不要有alert方法,不然在有些浏览器会进入死循环的.例如:$('#test').focus(function(){ alert('dead loop'); }); 在chrome下 ...
- SpringMVC无法获取请求中的参数的问题的调查与解决(1)
*更新:本文第一版中犯了比较大的错误,无论@RequestBody还是@RequestParam注解一样,都会使用全局的Encoding进行解码,会导致特殊编码的参数值丢失. 只要抛弃掉注解,就完全可 ...
- vim 图
- Linux文件查找
Linux下查找文件的命令有两个; locate: find : locate这个命令对其生成的数据库进行遍历(生成数据库的命令:updatedb),这一特性决定了查 找文件速度很快,但是locate ...
- 学习制作第一个 openfire 插件
本文地址:http://www.cnblogs.com/jying/p/3683409.html 蛋疼的自学路~~~ 开始想法是修改openfire源码,但修改后发现不好测试,不会发布,不会使用,各种 ...