collections模块
在内置数据类型(dict、list、set、tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter、deque、defaultdict、namedtuple和OrderedDict等。

1.namedtuple: 生成可以使用名字来访问元素内容的tuple
# from collections import Iterator  #迭代器
# from collections import Iterable  #可迭代对象

point1 = (1, 2)
x = point1[0]
y = point1[1]
print(x)
print(y)

# from collections import namedtuple
# P = namedtuple('Point',['x','y'])
# p1 = P(1,2)
# print(p1.x)
# print(p1.y)

# p2 = P(3,4)
# print(p2.x)
# print(p2.y)

例子:

from collections import namedtuple
Card = namedtuple('card',['suits','rank'])
                 (card,描述性的字符串,没什么用)
c1 = Card("红桃", "2")
print(c1.suits)

# 描述一类东西的时候,这一类东西都有相同的特征。
# 想直接用特征的名字就描述这个值的时候,就可以用可命名元祖
# 面向对象的时候还会再讲
# 生日:年月日  (2011,11,11)

#练习:

from collections import namedtuple
brithday = namedtuple('brd', ['year', 'month', 'day'])
lifu = brithday("1995", "3", "29")
print(lifu.year)
print(lifu.month)
print(lifu.day)

2.deque: 双端队列,可以快速的从另外一侧追加和推出对象
deque是为了高效实现插入和删除操作的双向列表,
#队列:先进先出

import queue   #队列_多线程多进程
q = queue.Queue()
q.put([1])
q.put(2)      #处理任务
q.put(300)
q.put('aaa')
q.put('wahaha')
print(q.qsize())
输出;
5
# print(q.get())
输出:
[1]
# print(q.get())
输出:
2
# print(q.get())
输出:
300
# print(q.get())
输出:
aaa
# print(q.get())   #hold住的功能
输出:
wahaha
# print(q.qsize())
输出:
0
# print(q.get_nowait())  #如果没有不会hold住,且会报错

 

from collections import deque
dq = deque()
dq.append('a')
dq.append('b')
dq.appendleft('c')
print(dq)
print(dq.popleft())
输出:
deque(['c', 'a', 'b'])
c

3.Counter: 计数器,主要用来计数 Counter类的目的是用来跟踪值出现的次数。它是一个无序的容器类型,以字典的键值对形式存储,其中元素作为key,其计数作为value。计数值可以是任意的Interger(包括0和负数)。Counter类和其他语言的bags或multisets很相似。

from collections import Counter
c = Counter('abcdeabcdabcaba')
print(c)
输出:Counter({'a': 5, 'b': 4, 'c': 3, 'd': 2, 'e': 1})

4.OrderedDict: 有序字典
d = dict([('a', 1), ('b', 2), ('c', 3)])
print(d)
输出:
{'a': 1, 'b': 2, 'c': 3}

from collections import OrderedDict
od = OrderedDict([('a', [1,2,3,4]), ('b', 2), ('c', 3)])
print(od)

输出:
OrderedDict([('a', [1, 2, 3, 4]), ('b', 2), ('c', 3)])

 

# from collections import OrderedDict
# od = OrderedDict([('a', [1,2,3,4]), ('b', 2), ('c', 3)])
# for k in od:
#     print(k,od[k])

输出:
a [1, 2, 3, 4]
b 2
c 3

 

from collections import OrderedDict
od = OrderedDict([('a', 1), ('b', 2), ('c', 3)])
for key in od:
    print(key)
输出:
a
b
c

od["key"] = "value"
for key in od:
    print(key)
输出:
a
b
c
key

5.defaultdict: 带有默认值的字典

l = [11,22,33,44,55,66,77,88,99,90]
dic = {"k1":[], "k2":[]}
for i in l:
    if i > 66:
        dic["k1"].append(i)
    else:
        dic["k2"].append(i)
print(dic)


# l = [11,22,33,44,55,66,77,88,99,90]
# dic = {}

l = [11,22,33,44,55,66,77,88,99,90]
dic = {}
for i in l:
    if i > 66:
        if "k1" in dic:
            dic["k1"].append(i)
        else:
            dic["k1"] = []
            dic["k1"].append(i)
print(dic)

from collections import defaultdict
values = [11, 22, 33,44,55,66,77,88,99,90]
my_dict = defaultdict(list)
for value in  values:
    if value>66:
        my_dict['k1'].append(value)
    else:
        my_dict['k2'].append(value)
print(my_dict)