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》