collections标准库

collections标准库

之前Python的第三方库用的一直很舒服,现在突然发现标准库也有collections这样的神器,可以补充list、set、dict以外的应用

1. namedtuple

namedtuple是一个函数,返回一个自定义的tuple,自定义tuple是通过属性而不是索引,使用体验和C的结构体类似

# coding=utf-8
from collections import namedtuple

Point = namedtuple('Point', ('x', 'y', 'color'))
print(Point)  # <class '__main__.Point'>
p = Point(1, 2, 'red')
print(p.x, p.y, p.color)  # 1 2 red
p2 = p._replace(x=12)  # 这里的更新是返回一个新的Point,p本身不变,因为tuple不可变
print(p, p2)  # Point(x=1, y=2, color='red') Point(x=12, y=2, color='red')

2. deque

deque特别适合用于队列和栈操作。虽然list也可以模拟队列和栈,但是list是线性存储的单链表数据结构,适合按索引访问,但插入和删除的效率就比较低,而·基于双向链表的deque特别适合做这些操作

# coding=utf-8
from collections import deque

q = deque(['a', 'b', 'c'])
q.append('x')  # 尾部添加
q.appendleft('y')  # 头部添加
print(q)  # deque(['y', 'a', 'b', 'c', 'x'])
print(q.pop())  # x 尾部弹出
print(q.popleft())  # y 头部弹出
print(q)  # deque(['a', 'b', 'c'])

q.insert(1, 'insert')  # 插入
q.remove('b')  # 删除指定元素
print(q)  # deque(['a', 'insert', 'c'])
print(q[1])  # insert 索引

# for循环遍历
for i, e in enumerate(q, start=1):
    print(i, ':', e)
# 1 : a
# 2 : insert
# 3 : c

3.defaultdict

普通的dict结构在访问key不存在的对象时会报error,defaultdict可以通过设定默认值解决这个问题,个人感觉在写状态机的时候,初始状态用defaultdict特别方便,有点像C语言的static 变量,只初始一次

# coding=utf-8
from collections import defaultdict

dd = defaultdict(lambda: 10, {})  # 构造函数里的{}可以初始化dd的字典内容
dd['a'] = 10
dd['b'] += 100
print(dd)  # defaultdict(<function <lambda> at 0x00000000052C4D08>, {'a': 10, 'b': 110})


# 字符统计的例子
def zero():
    return 0


# 生成随机数字串
def generate_digits(length=15):
    import string
    import random
    return random.sample(length * string.digits, length)


chs = generate_digits(5)
stat = defaultdict(zero)
for ch in chs:
    stat[ch] += 1
for k, v in stat.items():
    print(k, ':', v)
# 2 : 2
# 3 : 1
# 9 : 1
# 5 : 1

4. OrderedDict

普通的dict是无须的,也就是在遍历的时候是无能确定key的顺序的,OrderDict会根据插入顺序排序,可以用在自己封装的对象中,如pytorch中的torch.nn.Sequence就利用了这个特性

# coding=utf-8
from collections import OrderedDict

od = OrderedDict([
    ('a', 1),
    ('b', 2),
    ('c', 3),
])
od['hello'] = 0
print(od)  # OrderedDict([('a', 1), ('b', 2), ('c', 3), ('hello', 0)])
for k, v in od.items():
    print(k, ':', v)
# a : 1
# b : 2
# c : 3
# hello : 0

5. ChainMap

ChainMap可以将多个dict合并,联合查找

# coding=utf-8
from collections import ChainMap

a = {'a': 1, 'b': 2}
b = {'b': 3, 'd': 4}  # 注意这里的b重复
cm = ChainMap(a, b)
print(cm)  # ChainMap({'a': 1, 'b': 2}, {'b': 3, 'd': 4})
print(cm['b'])  # 2 取第一个
print(cm['d'])  # 4
cm['b'] = -2  # 这里修改的也是第一个b,由于是传引用,会修改a中的值
print(a, b)  # {'a': 1, 'b': -2} {'b': 3, 'd': 4}

6. Counter

就和名称一样,Counter是个简单的计数器

# coding=utf-8
from collections import Counter

c = Counter('helloworld')  # 统计可迭代对象的个数,并自动排序
print(c.most_common(2))  # [('l', 3), ('o', 2)]
for k, v in c.items():
    print(k, ':', v)
# h : 1
# e : 1
# l : 3
# o : 2
# w : 1
# r : 1
# d : 1

猜你喜欢

转载自www.cnblogs.com/fanghao/p/9510578.html