python标准库之collections介绍
collections----容器数据类型
collections模块包含了除list、dict、和tuple之外的容器数据类型,如counter、defaultdict、deque、namedtuple、orderdict,下面将一一介绍。
Counter
初始化:
Counter 支持三种形式的初始化。它的构造函数可以调用序列,一个字典包含密钥和计数,或使用关键字参数映射的字符串名称。
import collections print (collections.Counter(['a', 'b', 'c', 'a', 'b', 'b']))
print (collections.Counter({'a':2, 'b':3, 'c':1}))
print (collections.Counter(a=2, b=3, c=1))
输出结果:
Counter({'b': 3, 'a': 2, 'c': 1})
Counter({'b': 3, 'a': 2, 'c': 1})
Counter({'b': 3, 'a': 2, 'c': 1})
空的Counter容器可以无参数构造,并采用update()方法进行更新
import collections c = collections.Counter()
print ('Initial :', c) c.update('abcdaab')
print ('Sequence:', c) c.update({'a':1, 'd':5})
print ('Dict :', c)
输出结果:
Initial : Counter()
Sequence: Counter({'a': 3, 'b': 2, 'c': 1, 'd': 1})
Dict : Counter({'d': 6, 'a': 4, 'b': 2, 'c': 1})
访问计数:
当一个Counter被构造成功,它的值可以采用字典进行访问
import collections
c = collections.Counter('abcdaab')
for letter in 'abcde':
print ('%s : %d' % (letter, c[letter]))
结果:
a : 3
b : 2
c : 1
d : 1
e : 0
elements()方法可以返回一个包含所有Counter数据的迭代器
import collections
c = collections.Counter('extremely')
c['z'] = 0
print(c)
print (list(c.elements()))
Counter({'e': 3, 'm': 1, 'l': 1, 'r': 1, 't': 1, 'y': 1, 'x': 1, 'z': 0})
['e', 'e', 'e', 'm', 'l', 'r', 't', 'y', 'x']
most_common()返回前n个最多的数据
import collections
c=collections.Counter('aassdddffff')
for letter, count in c.most_common(2):
print ('%s: %d' % (letter, count))
f: 4
d: 3
算法:
Counter实例支持聚合结果的算术和集合操作。
import collections c1 = collections.Counter(['a', 'b', 'c', 'a', 'b', 'b'])
c2 = collections.Counter('alphabet') print ('C1:', c1)
print ('C2:', c2) print ('\nCombined counts:')
print (c1 + c2) print ('\nSubtraction:')
print (c1 - c2) print ('\nIntersection (taking positive minimums):')
print (c1 & c2) print ('\nUnion (taking maximums):')
print (c1 | c2)
C1: Counter({'b': 3, 'a': 2, 'c': 1})
C2: Counter({'a': 2, 'l': 1, 'p': 1, 'h': 1, 'b': 1, 'e': 1, 't': 1})
Combined counts:
Counter({'a': 4, 'b': 4, 'c': 1, 'l': 1, 'p': 1, 'h': 1, 'e': 1, 't': 1})
Subtraction:
Counter({'b': 2, 'c': 1})
Intersection (taking positive minimums):
Counter({'a': 2, 'b': 1})
Union (taking maximums):
Counter({'b': 3, 'a': 2, 'c': 1, 'l': 1, 'p': 1, 'h': 1, 'e': 1, 't': 1})
defaultdict
标准字典包括setdefault方法()获取一个值,如果值不存在,建立一个默认。相比之下,defaultdict允许调用者在初始化时预先设置默认值。
import collections def default_factory():
return 'default value' d = collections.defaultdict(default_factory, foo='bar')
print ('d:', d)
print ('foo =>', d['foo'])
print ('x =>', d['x'])
d: defaultdict(<function default_factory at 0x000002567E713E18>, {'foo': 'bar'})
foo => bar
x => default value
Deque
双端队列,支持从两端添加和删除元素。更常用的栈和队列是退化形式的双端队列,仅限于一端在输入和输出。
import collections
d = collections.deque('abcdefg')
print ('Deque:', d)
print ('Length:', len(d))
print ('Left end:', d[0])
print ('Right end:', d[-1])
d.remove('c')
print ('remove(c):', d)
Deque: deque(['a', 'b', 'c', 'd', 'e', 'f', 'g'])
Length: 7
Left end: a
Right end: g
remove(c): deque(['a', 'b', 'd', 'e', 'f', 'g'])
双端队列从左右两端插入数据
import collections # 右端插入
d = collections.deque()
d.extend('abcdefg')
print ('extend :', d)
d.append('h')
print ('append :', d) # 左端插入
d = collections.deque()
d.extendleft('abcdefg')
print ('extendleft:', d)
d.appendleft('h')
print ('appendleft:', d)
extend : deque(['a', 'b', 'c', 'd', 'e', 'f', 'g'])
append : deque(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])
extendleft: deque(['g', 'f', 'e', 'd', 'c', 'b', 'a'])
appendleft: deque(['h', 'g', 'f', 'e', 'd', 'c', 'b', 'a'])
类似地,双端队列的元素可以从两端获取。
import collections
print ('From the right:')
d = collections.deque('abcdefg')
while True:
try:
print (d.pop())
except IndexError:
break
print ('\nFrom the left:')
d = collections.deque('abcdefg')
while True:
try:
print (d.popleft())
except IndexError:
break
From the right:
g
f
e
d
c
b
a From the left:
a
b
c
d
e
f
g
由于双端队列是线程安全的,在单独的线程中内容甚至可以从两端同时消费。
import collections
import threading
import time candle = collections.deque(xrange(11)) def burn(direction, nextSource):
while True:
try:
next = nextSource()
except IndexError:
break
else:
print ('%8s: %s' % (direction, next))
time.sleep(0.1)
print ('%8s done' % direction)
return left = threading.Thread(target=burn, args=('Left', candle.popleft))
right = threading.Thread(target=burn, args=('Right', candle.pop)) left.start()
right.start() left.join()
right.join()
Left: 0
Right: 10
Right: 9
Left: 1
Right: 8
Left: 2
Right: 7
Left: 3
Right: 6
Left: 4
Right: 5
Left done
Right done
队列的另一个有用的功能是在任意方向旋转,通俗来讲就是队列的左移右移
import collections d = collections.deque(xrange(10))
print ('Normal :', d) d = collections.deque(xrange(10))
d.rotate(2)
print ('Right rotation:', d) d = collections.deque(xrange(10))
d.rotate(-2)
print ('Left rotation :', d)
Normal : deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Right rotation: deque([8, 9, 0, 1, 2, 3, 4, 5, 6, 7])
Left rotation : deque([2, 3, 4, 5, 6, 7, 8, 9, 0, 1])
namedtuple
标准的元组使用数值索引来访问其成员
bob = ('Bob', 30, 'male')
print ('Representation:', bob)
jane = ('Jane', 29, 'female')
print ('\nField by index:', jane[0])
print ('\nFields by index:')
for p in [ bob, jane ]:
print ('%s is a %d year old %s' % p)
Representation: ('Bob', 30, 'male')
Field by index: Jane
Fields by index:
Bob is a 30 year old male
Jane is a 29 year old femal
记住每个索引对应的值是很容易出错的,尤其是在元组有多个元素的情况下。namedtuple为每个成员分配了名字。
import collections
Person = collections.namedtuple('Person', 'name age gender')
print ('Type of Person:', type(Person))
bob = Person(name='Bob', age=30, gender='male')
print ('\nRepresentation:', bob)
jane = Person(name='Jane', age=29, gender='female')
print ('\nField by name:', jane.name)
print ('\nFields by index:')
for p in [ bob, jane ]:
print ('%s is a %d year old %s' % p)
Type of Person: <type 'type'> Representation: Person(name='Bob', age=30, gender='male') Field by name: Jane Fields by index:
Bob is a 30 year old male
Jane is a 29 year old female
字段名称解析,无效值会导致ValueError
import collections try:
collections.namedtuple('Person', 'name class age gender')
except ValueError, err:
print (err) try:
collections.namedtuple('Person', 'name age gender age')
except ValueError, err:
print (err)
Type names and field names cannot be a keyword: 'class'
Encountered duplicate field name: 'age'
OrderedDict
OrderedDict是字典子类,记得其内容被添加的顺序
import collections
print ('Regular dictionary:')
d = {}
d['a'] = 'A'
d['b'] = 'B'
d['c'] = 'C'
d['d'] = 'D'
d['e'] = 'E'
for k, v in d.items():
print( k, v)
print ('\nOrderedDict:')
d = collections.OrderedDict()
d['a'] = 'A'
d['b'] = 'B'
d['c'] = 'C'
d['d'] = 'D'
d['e'] = 'E'
for k, v in d.items():
print (k, v)
Regular dictionary:
a A
c C
b B
e E
d D OrderedDict:
a A
b B
c C
d D
e E
参考来源:https://pymotw.com/2/collections/index.html#module-collections
python标准库之collections介绍的更多相关文章
- python标准库之glob介绍
python标准库之glob介绍 glob 文件名模式匹配,不用遍历整个目录判断每个文件是不是符合. 1.通配符 星号(*)匹配零个或多个字符 import glob for name in glob ...
- python标准库:collections和heapq模块
http://blog.csdn.net/pipisorry/article/details/46947833 python额外的数据类型.collections模块和heapq模块的主要内容. 集合 ...
- Python标准库:1. 介绍
标准库包括了几种不同类型的库. 首先是那些核心语言的数据类型库,比方数字和列表相关的库.在核心语言手冊里仅仅是描写叙述数字和列表的编写方式,以及它的排列,而未定义它的语义. 换一句话说,核心语言手冊仅 ...
- 【python】Python标准库defaultdict模块
来源:http://www.ynpxrz.com/n1031711c2023.aspx Python标准库中collections对集合类型的数据结构进行了很多拓展操作,这些操作在我们使用集合的时候会 ...
- Python标准库——collections模块的Counter类
1.collections模块 collections模块自Python 2.4版本开始被引入,包含了dict.set.list.tuple以外的一些特殊的容器类型,分别是: OrderedDict类 ...
- python第六天 函数 python标准库实例大全
今天学习第一模块的最后一课课程--函数: python的第一个函数: 1 def func1(): 2 print('第一个函数') 3 return 0 4 func1() 1 同时返回多种类型时, ...
- Python 标准库一览(Python进阶学习)
转自:http://blog.csdn.net/jurbo/article/details/52334345 写这个的起因是,还是因为在做Python challenge的时候,有的时候想解决问题,连 ...
- Python标准库的学习准备
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Python标准库是Python强大的动力所在,我们已经在前文中有所介绍.由于标准 ...
- Python标准库——走马观花
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Python有一套很有用的标准库(standard library).标准库会随着 ...
随机推荐
- 浅谈无旋treap(fhq_treap)
一.简介 无旋Treap(fhq_treap),是一种不用旋转的treap,其代码复杂度不高,应用范围广(能代替普通treap和splay的所有功能),是一种极其强大的平衡树. 无旋Treap是一个叫 ...
- ICEM-带死角弯管
原视频下载地址:https://yunpan.cn/cqRiHaQiLi8I7 访问密码 b5c6
- Git .gitignore中已添加文件路径,但仍未被忽略
当文件之前已经被提交到仓库后,后面即使将文件路径添加到 .gitignore ,使用 git status 命令,依然会看到文件被修改. $ git status 位于分支 master 您的分支与上 ...
- c3p0数据库连接池 原创: Java之行 Java之行 5月8日 一、连接池概述 实际开发中“获得连接”或“释放资源”是非常消耗系统资源的两个过程
c3p0数据库连接池 原创: Java之行 Java之行 5月8日 一.连接池概述 实际开发中“获得连接”或“释放资源”是非常消耗系统资源的两个过程 DB连接池HikariCP为什么如此快 原创: D ...
- [原][OSG][osgEarth]osgEarth例子程序简介
1.osgearth_graticule:生成经纬线. 2.osgearth_annotation:各类标注(点.线.面.模型.文本等). 3.osgearth_city:加载一个城市三维模型,可以浏 ...
- React调试——visual studio code
原文链接:Using React in Visual Studio Code 原文链接:Live edit and debug your React apps directly from VS Cod ...
- Mysql5.7数据库介绍
(1).默认数据库介绍 information_schema 这个数据库保存了Mysql服务器所有数据库的信息.如数据库名.数据库的表.表栏的数据类型访问权限等. mysql 这个库是系统库,里面保存 ...
- ubuntu 16.04 修改网卡显示名称
~# sudo nano /etc/default/grub找到:GRUB_CMDLINE_LINUX=""改为:GRUB_CMDLINE_LINUX="net.ifna ...
- js下利用userData实现客户端保存表单数据
对于多数网页制作的朋友,实现在客户端保存在网页表单上的信息,比较多的是采用Cookie技术来实现,这些功能例如:下拉列表框选择的选项,文本框输入的数据等. 事实上,我们可以利用微软DHTML默认行为中 ...
- 转载:Spark中文指南(入门篇)-Spark编程模型(一)
原文:https://www.cnblogs.com/miqi1992/p/5621268.html 前言 本章将对Spark做一个简单的介绍,更多教程请参考:Spark教程 本章知识点概括 Apac ...