Date: 2019-05-27

Author: Sun

Collections库

​ Python拥有一些内置的数据类型,比如str, int, list, tuple, dict等, collections模块在这些内置数据类型的基础上,提供了几个额外的数据类型:

  • namedtuple(): 生成可以使用名字来访问元素内容的tuple子类
  • deque: 双端队列,可以快速的从另外一侧追加和推出对象
  • Counter: 计数器,主要用来计数
  • OrderedDict: 有序字典
  • defaultdict: 带有默认值的字典

1. namedtuple

namedtuple主要用来产生可以使用名称来访问元素的数据对象,通常用来增强代码的可读性

创建类实例

比如我们用户拥有一个这样的数据结构,每一个对象是拥有三个元素的tuple。

使用namedtuple方法就可以方便的通过tuple来生成可读性更高也更好用的数据结构。

# -*- coding: utf-8 -*-
__author__ = 'sun'
__date__ = '2019/5/27 0:19' from collections import namedtuple website_list = [
('Sohu', 'http://www.google.com/', u'张朝阳'),
('Sina', 'http://www.sina.com.cn/', u'王志东'),
('163', 'http://www.163.com/', u'丁磊')
] Website = namedtuple('Website', ['name', 'url', 'founder']) for website in website_list:
website = Website._make(website)
print(website)

2. deque

​ deque其实是双向队列的缩写,翻译过来就是双端队列,它最大的好处就是实现了从队列 头部快速增加和取出对象: .popleft(), .appendleft() 。

​ 作为一个双端队列,deque还提供了一些其他的好用方法,比如 rotate

raw = [1,2,3]
d = collections.deque(raw)
print(d) #结果deque([1, 2, 3]) #右增
d.append(4)
print(d) #结果deque([1, 2, 3, 4])
#左增
d.appendleft(0)
print(d) #结果deque([0, 1, 2, 3, 4]) #左扩展
d.extend([5,6,7])
print(d) #结果deque([0, 1, 2, 3, 4, 5, 6, 7])
#右扩展
d.extendleft([-3,-2,-1])
print(d) #结果deque([-1, -2, -3, 0, 1, 2, 3, 4, 5, 6, 7]) #右弹出
r_pop = d.pop()
print(r_pop) #结果7
print(d) #结果deque([-1, -2, -3, 0, 1, 2, 3, 4, 5, 6])
#左弹出
l_pop = d.popleft()
print(l_pop) #结果-1
print(d) #结果deque([-2, -3, 0, 1, 2, 3, 4, 5, 6]) #将右边n个元素值取出加入到左边
print(d) #原队列deque([-2, -3, 0, 1, 2, 3, 4, 5, 6])
d.rotate(3)
print(d) #rotate以后为deque([4, 5, 6, -2, -3, 0, 1, 2, 3])

案例:一个无尽循环的跑马灯

# -*- coding: utf-8 -*-
__author__ = 'sun'
__date__ = '2019/5/27 0:19' import sys
import time
from collections import deque fancy_loading = deque('>--------------------') while True:
print('\r%s' % ''.join(fancy_loading))
fancy_loading.rotate(1)
sys.stdout.flush()
time.sleep(0.08)

3. Counter

​ Counter类的目的是用来跟踪值出现的次数。它是一个无序的容器类型,以字典的键值对形式存储,其中元素作为key,其计数作为value。计数值可以是任意的Interger(包括0和负数)

from collections import Counter
>>> c = Counter("abcdcba")
>>> c
Counter({'a': 2, 'c': 2, 'b': 2, 'd': 1})
>>> c["b"] = 0
>>> c
Counter({'a': 2, 'c': 2, 'd': 1, 'b': 0})
>>> del c["a"]
>>> c
Counter({'c': 2, 'b': 2, 'd': 1})

常见支持方法:

(1)elements()

elements就是将其中的key值乘以出现次数全部打印出来

from collections import Counter

c = Counter(cats=4, dogs=8, mouse=2)
print(list(c.elements())) #输出结果:['cats', 'cats', 'cats', 'cats', 'dogs', 'dogs', 'dogs', 'dogs', 'dogs', 'dogs', 'dogs', 'dogs','mouse', 'mouse']

(2)most_common([n])

​ n为可选参数,如果不输入n的值,则默认返回所有,输入-1则返回空,输入小于最长长度,则返回前n个数,输入等于最长长度,则返回所有

# 获取出现频率最高的2个字符
print(c.most_common(2))

案例: 使用Counter模块统计一段句子里面所有字符出现次数

# -*- coding: utf-8 -*-
__author__ = 'sun'
__date__ = '2019/5/27 0:19' 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. Counts are allowed to be any integer value including zero or negative counts. The Counter class is similar to bags or multisets in other languages.'''.lower() c = Counter(s)
# 获取出现频率最高的5个字符
print(c.most_common(5))

4 OrderedDict

​ 在Python中,dict这个数据结构由于hash的特性,是无序的,这在有的时候会给我们带来一些麻烦, 幸运的是,collections模块为我们提供了OrderedDict,当你要获得一个有序的字典对象时,用它就对了。

from collections import OrderedDict

d = OrderedDict(a=11, b=2, c=3)

print(d)    #会排序

5 defaultdict

​ 在使用Python原生的数据结构dict的时候,如果用 d[key] 这样的方式访问, 当指定的key不存在时,是会抛出KeyError异常的。

​ 但是,如果使用defaultdict,只要你传入一个默认的工厂方法,那么请求一个不存在的key时, 便会调用这个工厂方法使用其结果来作为这个key的默认值。

defaultdict很适合应用在一个key对应value list场合。

# -*- coding: utf-8 -*-
__author__ = 'sun'
__date__ = '2019/5/27 0:19' from collections import defaultdict members = [
# Age, name
['male', 'John'],
['male', 'Jack'],
['female', 'Lily'],
['male', 'Pony'],
['female', 'Lucy'],
]
#{
# ‘male’:['john', 'jack','pony']
#}
result = defaultdict(list)
for sex, name in members:
result[sex].append(name) print(result)

​ 合并列表元素,并排序

from collections import defaultdict
s=[('yellow',1),('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
d=defaultdict(list)
for k, v in s:
d[k].append(v)
a=sorted(d.items())
print(a)

​ defaultdict还可以被用来计数,将default_factory设为int即可

​ 字符串中的字母第一次出现时,字典中没有该字母,default_factory函数调用int()为其提供一个默认值0,加法操作将计算出每个字母出现的次数。

from collections import defaultdict
s = 'i am an chinese man, i love china.'
d = defaultdict(int)
for k in s:
d[k] += 1
print('\n',d)
a=sorted(d.items())
print('\n',a)

Collections库使用的更多相关文章

  1. Robot Framework学习笔记(五)------Collections 库

    Collections 库同样为 Robot Framework 标准类库,它所提供的关键字主要用于列表.索引.字典的处理. 1.添加类 在使用之前需要在测试套件(项目)中添加 2.创建字典 字典也是 ...

  2. $python collections库

    collections库是python内置的集合库,本文主要讲解以下5种数据结构的用法: namedtuple 命名元组,是tuple的子类 deque 双向列表 defaultdict 有默认值的字 ...

  3. Robot Framework(Collections 库)

    Collections 库 Collections 库同样为Robot Framework 标准类库,它所提供的关键字主要用于列表.索引.字典的处理. 在使用之前需要在测试套件(项目)中添加:

  4. robot_framewok自动化测试--(6)Collections 库

    Collections 库 Collections 库同样为 Robot Framework 标准类库,它所提供的关键字主要用于列表.索引.字典的处理. 在使用之前需要在测试套件(项目)中添加: 1. ...

  5. Screenshot 库和Collections 库

    一.screenShot 是 robot framework的标准类库,用于截取当前窗口,需要手动加载. 示例: 运行结果: 二.Collections 库 Collections 库同样为 Robo ...

  6. collections库的namedtuple+pytest的使用

    from collections import namedtupleTask=namedtuple('Task',['summary','owner','done','id'])Task.__new_ ...

  7. 【RF库Collections库测试】关键字append to list

    Arguments:[ list_ | *values ]Adds `values` to the end of `list`.

  8. robotframework的学习笔记(十五)----robotframework标准库Collections

    Collections库是RobotFramework用来处理列表和字典的库,官方文档是这样介绍的:A test library providing keywords for handling lis ...

  9. Robot framework(RF) Builti,Screenshot和Collections标准库介绍

    1.1  Builti标准类库 在学习一门编程语言的时候,大多教材都是从打印“hello world”开始.我们可以像编程语言一样来学习Robot Framework.虽然通过RIDE 提供“填表”一 ...

随机推荐

  1. python 一句话输出26个英文字母

    chr(i) # return i character ord(c) # return integer >>> [chr(i) for i in range(97,123)] ['a ...

  2. swift使用查阅资料备份4

    Swift - RxSwift的使用详解6(观察者2: 自定义可绑定属性) http://www.hangge.com/blog/cache/detail_1946.html extension UI ...

  3. 理解UIView的绘制

    界面的绘制和渲染 UIView是如何到显示的屏幕上的. 这件事要从RunLoop开始,RunLoop是一个60fps的回调,也就是说每16.7ms绘制一次屏幕,也就是我们需要在这个时间内完成view的 ...

  4. Unity 引用的玩家不受控制

    原因是因为从Project拖过去的, 应该从Hierarchy拖过去.

  5. Unity设置Turorials-Wide布局

    在bilibili上看关于UGUI官翻视频时,很喜欢对方的布局,自己照着改了下,改的步骤如下: 1, 先选用 2by3 布局 2, 把Project面板拖到Hierarchy下边 3, 把2列布局改成 ...

  6. CF 689D - Friends and Subsequences

    689D - Friends and Subsequences 题意: 大致跟之前题目一样,用ST表维护a[]区间max,b[]区间min,找出多少对(l,r)使得maxa(l,r) == minb( ...

  7. JS - 浅拷贝与深拷贝的理解以及简单实现方法

    前几天撸项目代码时, 由一个技术点间接牵扯出了这东西. 所以就来总结一下. 深拷贝 拷贝对象每个层级的属性. 作用的对象是 js中引用类型的对象,基本类型没有涉及. 本质上将引用类型的对象在堆上重新开 ...

  8. HDU 1061 Rightmost Digit( 快速幂水 )

    链接:传送门 题意:求 N^N 的个位 思路:快速幂水题 /********************************************************************** ...

  9. 实战:vue项目中导入swiper插件

    版本选择 swiper是个常用的插件,现在已经迭代到了第四代:swiper4.常用的版本是swiper3和swiper4,我选择的是swiper3. 安装 安装swiper3的最新版本3.4.2: n ...

  10. Qt Designer设计 UI 文件并调用

    本文介绍的是Qt Designer设计 UI 文件并调用,在坛子里逛了一圈,关于UI方面的好像不怎多,本篇给大家分享一下. AD: 2013云计算架构师峰会超低价抢票中 Qt Designer设计 U ...