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)具有不可变性,但是单独的元组在无法满足现有需求时,可以使 ...
随机推荐
- MT【114】构造二次函数
评:b+c,bc好比向量里的一组基底,可以将关于b,c的对称式表示出来.
- 【刷题】洛谷 P4319 变化的道路
题目描述 小 w 和小 c 在 H 国,近年来,随着 H 国的发展,H 国的道路也在不断变化着 根据 H 国的道路法,H 国道路都有一个值 \(w\) ,表示如果小 w 和小 c 通过这条道路,那么他 ...
- Android Progurad 代码混淆
ref: ProGuard基础语法和打包配置.mdhttps://github.com/D-clock/Doc/blob/master/Android/Gradle/3_ProGuard%E5%9F% ...
- bzoj3926: [Zjoi2015]诸神眷顾的幻想乡 广义后缀自动机模板
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #d ...
- [USACO18OPEN]Talent Show
题目描述 Farmer John要带着他的N头奶牛,方便起见编号为1…N,到农业展览会上去,参加每年的达牛秀!他的第iii头奶牛重量为wi,才艺水平为ti,两者都是整数. 在到达时,Farmer J ...
- [THUSC 2016] 补退选 (Trie树)
link $solution:$ $Trie$树很显然吧,那么如何去处理每次询问.对于$Trie$树的每个节点放一个$vector$表示其若有$v$个人的最小时间. #include<iostr ...
- Win10不能将文件夹固定到任务栏
Win10无法将文件夹锁定到任务栏的解决方法: 1.点开始——在运行里输入%APPDATA%\Microsoft\Windows\Recent\AutomaticDestinations,按回车键 ...
- D. Mahmoud and Ehab and the binary string Codeforces Round #435 (Div. 2)
http://codeforces.com/contest/862/problem/D 交互题 fflush(stdout) 调试: 先行给出结果,函数代替输入 #include <cstdio ...
- Activity工作流(2)-入门安装运行第一个例子
转: Activity工作流(2)-入门安装运行第一个例子 置顶 2017年05月24日 15:58:50 li_ch_ch 阅读数:24432 版权声明:本文为博主原创文章,未经博主允许不得转载 ...
- C++并发类成员函数调用(练习1)
一般类成员函数开线程格式 std::thread t1(&类名::函数,&实例化对象,参数....) ||std::thread t1(std::bind(&&类名:: ...