# collections 数据类型

collections 数据类型主要是为了弥补 list /tuple / dict 的额外数据类型

## ChainMap

``````import collections
## 赋值，合并字典的作用
a = {'a':"A"}
b = {"b":"B"}

m = collections.ChainMap(a,b)
for k,v in m.items():
print('key: {} | value:{}'.format(k,v))

print(m)
``````

``````a = {"a":"A","b":"B"}
b = {"b":"C"}
m2 = collections.ChainMap(a,b)
print(m2)

for k,v in m2.items():
print('key: {} | value:{}'.format(k,v))

``````

``````ChainMap({'a': 'A', 'b': 'B'}, {'b': 'C'})
key: a | value:A
key: b | value:B
``````

## Counter

Counter 顾名思义，就是计算总数的意思，可以计算出一个序列中每个元素的个数，一个简单的例子

``````>>> import collections
>>> collections.Counter("Hello World")
Counter({'l': 3, 'o': 2, 'H': 1, 'e': 1, ' ': 1, 'W': 1, 'r': 1, 'd': 1})
``````

``````>>> a =  collections.Counter("Hello World")
>>> a['W']
1
``````

``````import collections

c1 = collections.Counter("Hello World")
c2 = collections.Counter("Hello Python")

print("c1 + c2 =",c1 + c2)
print("c1 - c2 = ",c1 - c2)
print("c1 | c2 = ",c1 | c2)
print("c1 & c2 = ",c1 & c2)
``````

``````c1 + c2 = Counter({'l': 5, 'o': 4, 'H': 2, 'e': 2, ' ': 2, 'W': 1, 'r': 1, 'd': 1, 'P': 1, 'y': 1, 't': 1, 'h': 1, 'n': 1})
c1 - c2 =  Counter({'l': 1, 'W': 1, 'r': 1, 'd': 1})
c1 | c2 =  Counter({'l': 3, 'o': 2, 'H': 1, 'e': 1, ' ': 1, 'W': 1, 'r': 1, 'd': 1, 'P': 1, 'y': 1, 't': 1, 'h': 1, 'n': 1})
c1 & c2 =  Counter({'l': 2, 'o': 2, 'H': 1, 'e': 1, ' ': 1})
``````

## defaultdict

``````test = {"a":"b"}
test.get("a")
# 如果获取一个不存在的 key 值
test.get("b") # 返回None
# 但是通过get 可以指定一个 key 值
test.get("b","this is b") # 返回 this is b
``````

defaultdict 也差不多是这样的道理，当你获取一个不存在的 key 值的时候，返回默认值

``````import collections

def default_value():
return "Default value"

m = collections.defaultdict(default_value,foo='aaa')
print(m['fxx']) # 返回 Default value
``````

## deque

``````from collections import deque
d = deque("abcdefg")
print(d) # deque(['a', 'b', 'c', 'd', 'e', 'f', 'g'])
d.remove('c')
print(d) # deque(['a', 'b', 'd', 'e', 'f', 'g'])
d.append('h')
print(d) #deque(['a', 'b', 'd', 'e', 'f', 'g', 'h'])
d.appendleft("1")
print(d) #deque(['1', 'a', 'b', 'd', 'e', 'f', 'g', 'h'])
# 使用pop 获取队列中的值
d.pop()
print(d) # deque(['1', 'a', 'b', 'd', 'e', 'f', 'g'])
d.popleft()
print(d) # deque(['a', 'b', 'd', 'e', 'f', 'g'])
``````

``````from collections import deque
import time

# deque 也可以用线程通信
d1 = deque(range(1000))
while True:
try:
item = nextSource()
print("方向：{} 线程： {} 正在处理： {} ".format(direction,i,item))
except IndexError as e:
break
else:
time.sleep(1)

for tl in left_ts:
tl.start()

for tr in right_ts:
tr.start()

for tl in left_ts:
tl.join()

for tr in right_ts:
tr.join()

``````

## OrderedDict

``````d = {}
d['a']= 'A'
d['b'] = 3
d['c']= 1
d['d']='B'
d['c']='C'
for k,v in d.items():
print(k,'=>',v)
``````

``````a => A
b => 3
c => C
d => B
``````

``````d = OrderedDict()
d['a']= 'A'
d['b'] = 3
d['s']= 1
d['d']='B'
d['c']='C'
for k,v in d.items():
print(k,'=>',v)

``````

``````a => A
b => 3
s => 1
d => B
c => C
``````

# 参考

《The Python3 Standard Library By Example》