collections 数据类型主要是为了弥补 list /tuple / dict 的额外数据类型app
代码:this
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)
若是字典中有重复的key值线程
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))
输出:code
ChainMap({'a': 'A', 'b': 'B'}, {'b': 'C'}) key: a | value:A key: b | value:B
结论:
结果是没有合并,若是只是合并字典的值,仍是直接使用update便可, 这个模块不怎么会用到,了解便可orm
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
除此以外,Counter对象还支持直接运算rem
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)
输出:get
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})
众所周知,当须要获取一个字典的值,可使用 xx[key] 这样的形式去获取,若是key值不存在,那么就会抛出一个错误,因此大部分推荐的作法是,使用 get 方法来获取字典的值,好比:it
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
双端队列,元素能够从两端弹出,插入和删除操做限定在队列的两边进行
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 import threading # deque 也能够用线程通讯 d1 = deque(range(1000)) def task(direction,i,nextSource): while True: try: item = nextSource() print("方向:{} 线程: {} 正在处理: {} ".format(direction,i,item)) except IndexError as e: break else: time.sleep(1) right_ts = [threading.Thread(target=task,args=('right',i,d1.pop))for i in range(10)] left_ts = [threading.Thread(target=task,args=('left',i,d1.popleft)) for i in range(10)] 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()
使用字典的时候,其输出时,不必定按照当时添加的顺序输出,例如:
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
可是 OrderedDict 会
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
当须要使用dict 来做为运算和存储的时候,这就是一个比较有用的特色了。
以上这些在平常使用的时候若是不了解,不多会去用到,但若是想写出优雅,简洁的代码,这些概念会起到必定的帮助做用
《The Python3 Standard Library By Example》