Python之容器类Collections
容器类Collections
标签(空格分隔): Python进阶
- defaultdict
- counter
- deque
- namedtuple
defaultdict
defaultdict的作用是可以不用检查key是否存在,如果不存在可以直接创建。 而不像dict,会raise KeyError.
示例代码:
some_dict = {}
some_dict['colours'] = "yellow"
# Raises KeyError: 'colours'
示例代码2:
from collections import defaultdict
some_dict = defaultdict()
some_dict['colours'] = 'yellow'
# it works
defaultdict的经典用法
示例代码3:
from collections import defaultdict
colours = (
('Yasoob', 'Yellow'),
('Ali', 'Blue'),
('Arham', 'Green'),
('Ali', 'Black'),
('Yasoob', 'Red'),
('Ahmed', 'Silver'),
)
favourite_colours = defaultdict(list)
for name, colour in colours:
favourite_colours[name].append(colour)
Counter
Counter可以帮助计算一个iterable或者mapping中各个item的出现次数。
示例代码4:
from collections import Counter
colours = (
('Yasoob', 'Yellow'),
('Ali', 'Blue'),
('Arham', 'Green'),
('Ali', 'Black'),
('Yasoob', 'Red'),
('Ahmed', 'Silver'),
)
favs = Counter(name for name, colour in colours)
print(favs)
# Output: Counter({
# 'Yasoob': 2,
# 'Ali': 2,
# 'Arham': 1,
# 'Ahmed': 1
# })
deque
deque == double ended queue 双头队列
deque 可以从左/右 进元素和出元素。
示例代码5:
from collections import deque
d = deque()
d.append('1')
d.append('2')
d.append('3')
d.appendleft('4')
d
#output: deque([4, 1, 2, 3])
d = deque(range(5))
print d
# output: deque([0, 1, 2, 3, 4])
d.pop()
#output: 4
d.popleft()
#output: 0
print d
#output: deque([1,2,3])
d.extendleft([0])
d.extend([6,7,8])
print d
#output: deque([0, 1, 2, 3, 6, 7, 8])
namedtuple
tuple和list的区别就是tuple是不可变的,tuple在初始化好了之后,元素不能插入也不能删除.
而namedtuple也继承了不可变的性质,但是它有点像dict,元素是key-value形式的,也就是named的由来,在初始化之后,可以通过name来访问元素。
示例代码6:
from collections import namedtuple
#tuple_name = namedtuple('tuple_name', 'item1_name, item2_name,...')
Animal = namedtuple('Animal', 'name age type')
perry = Animal(name="perry", age=31, type="cat")
print(perry)
# Output: Animal(name='perry', age=31, type='cat')
print(perry.name)
# Output: 'perry'
perry.age=40
# Output: Traceback (most recent call last):
# File "", line 1, in
# AttributeError: can't set attribute
Python之容器类Collections的更多相关文章
- python模块介绍- collections(5)-OrderedDict 有序字典
1.3.5 OrderedDict 有序字典 OrderedDict是dict的子类,它记住了内容添加的顺序. import collections print 'Regular dictionary ...
- Python自建collections模块
本篇将学习python的另一个内建模块collections,更多内容请参考:Python学习指南 collections是Python内建的一个集合模块,提供了许多有用的集合类. namedtupl ...
- Python标准库——collections模块的Counter类
1.collections模块 collections模块自Python 2.4版本开始被引入,包含了dict.set.list.tuple以外的一些特殊的容器类型,分别是: OrderedDict类 ...
- python中的collections
python中有大量的内置模块,很多是属于特定开发的功能性模块,但collections是属于对基础数据的类型的补充模块,因此,在日常代码中使用频率更高一些,值得做个笔记,本文只做主要关键字介绍,详细 ...
- Python标准模块--collections
1.模块简介 collections包含了一些特殊的容器,针对Python内置的容器,例如list.dict.set和tuple,提供了另一种选择: namedtuple,可以创建包含名称的tuple ...
- Python基础、collections补充
collections collections是Python数据类型的补充,可以实现Counter计数.可命名元组(namedtuple).默认字典.有序字典.双向队列等功能 参考:http://py ...
- python模块之collections
我们都知道,Python拥有一些内置的数据类型,比如str, int, list, tuple, dict等, collections模块在这些内置数据类型的基础上,提供了几个额外的数据类型: (1) ...
- Python系列之Collections内置模块(1)
collections 是 python 的内置模块,源码位于 Lib/collections/__init__.py ,该模块提供了通用的数据容器. deque 容器对象 通过 from colle ...
- Python常用模块--collections
collections是Python中一个非常强大的容器数据模块. 1.创建升级版的元组--namedtupe Python的元组(1,2,3)具有不可变性,但是单独的元组在无法满足现有需求时,可以使 ...
随机推荐
- php将两张身份证图片合并到一张图
/** * @desc 合并身份证的正反面到同一张图片 * @author Jimmy * @date 2016-12-33 * @param $imageSrc0 身份证正面 * @param $i ...
- Udp广播的发送与接收(C#+UdpClient) 上篇
简介: Udp广播消息用在局域网的消息传递很方便.本文使用UdpClient类在WPF下实现Udp广播收发 发送: void MainWindow_Loaded(object sender, Rout ...
- 【BZOJ2024】舞会(动态规划,容斥,高精度)
[BZOJ2024]舞会(动态规划,容斥,高精度) 题面 BZOJ 洛谷 题解 这种关系显然要先排序才不会不想影响. 设\(f[i][j]\)表示前\(i\)个女生中,选了\(j\)个女生配对,并且女 ...
- 【BZOJ5334】数学计算(线段树)
[BZOJ5334]数学计算(线段树) 题面 BZOJ 洛谷 题解 简单的线段树模板题??? 咕咕咕. #include<iostream> #include<cstdio> ...
- 【转】关于在vim中的查找和替换
1,查找 在normal模式下按下/即可进入查找模式,输入要查找的字符串并按下回车. Vim会跳转到第一个匹配.按下n查找下一个,按下N查找上一个. Vim查找支持正则表达式,例如/vim$匹配行尾的 ...
- 观V8源码中的array.js,解析 Array.prototype.slice为什么能将类数组对象转为真正的数组?
在官方的解释中,如[mdn] The slice() method returns a shallow copy of a portion of an array into a new array o ...
- 解题:POI 2004 String
题面 首先我们要有一个明确的构造思路 对于非根节点,我们把子树连上来的线两两配对,这样如果它有奇数个子树就会剩一个,这时候把这根线传给父亲即可.对于根节点还是两两配对,但是注意如果它也有奇数个子树就不 ...
- 解题:SCOI 2010 序列操作
题面 线段树......模板题(雾? 然而两种标记会互相影响,必须保证每次只放一个(不然就不知道怎么放了),具体的影响就是: 翻转标记会使得覆盖标记一起翻转,下放的时候就是各种swap 覆盖标记会抹掉 ...
- PDF文档小技巧整理一览
1.福昕阅读器文档背景修改为保护眼睛的颜色? 1)文件 -> 偏好设置 -> 访问 -> 勾选 "改变文档颜色" 2)选择 '自定义颜色'->'页面背景颜色 ...
- Chapter 8(查找)
1.二分查找和插值查找 //************************Search.h*********************************** #ifndef SEARCH_H # ...