1. 程式人生 > >Python中collections模組的使用

Python中collections模組的使用

本文將詳細講解collections模組中的所有類,和每個類中的方法,從原始碼和效能的角度剖析。

一個模組主要用來幹嘛,有哪些類可以使用,看__init__.py就知道

'''This module implements specialized container datatypes providing
alternatives to Python's general purpose built-in containers, dict,
list, set, and tuple.

* namedtuple   factory function for creating tuple subclasses with named fields
* deque        list-like container with fast appends and pops on either end
* ChainMap     dict-like class for creating a single view of multiple mappings
* Counter      dict subclass for counting hashable objects
* OrderedDict  dict subclass that remembers the order entries were added
* defaultdict  dict subclass that calls a factory function to supply missing values
* UserDict     wrapper around dictionary objects for easier dict subclassing
* UserList     wrapper around list objects for easier list subclassing
* UserString   wrapper around string objects for easier string subclassing

'''

__all__ = ['deque', 'defaultdict', 'namedtuple', 'UserDict', 'UserList', 'UserString', 'Counter', 'OrderedDict', 'ChainMap'] 

collections模組實現一些特定的資料型別,可以替代Python中常用的內建資料型別如dict, list, set, tuple,簡單說就是對基本資料型別做了更上一層的處理。

一、deque

用途:雙端佇列,頭部和尾部都能以O(1)時間複雜度插入和刪除元素。類似於列表的容器

所謂雙端佇列,就是兩端都能操作,與Python內建的list區別在於:頭部插入與刪除的時間複雜度為O(1),來個栗子感受一下:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = 'liao gao xiang'

"""
保留最後n個元素
"""
from collections import deque def search(file, pattern, history=5): previous_lines = deque(maxlen=history) for l in file: if pattern in l: yield l, previous_lines # 使用yield表示式的生成器函式,將搜尋過程的程式碼和搜尋結果的程式碼解耦 previous_lines.append(l) with open(b'file.txt', mode='r', encoding='utf-8') as f: for line, prevlines in search(f, 'Python', 5): for pline in prevlines: print(pline, end='') print(line, end='') d = deque() d.append(1) d.append("2") print(len(d)) print(d[0], d[1]) d.extendleft([0]) print(d) d.extend([6, 7, 8]) print(d) d2 = deque('12345') print(len(d2)) d2.popleft() print(d2) d2.pop() print(d2) # 在佇列兩端插入或刪除元素時間複雜度都是 O(1) ,區別於列表,在列表的開頭插入或刪除元素的時間複雜度為 O(N) d3 = deque(maxlen=2) d3.append(1) d3.append(2) print(d3) d3.append(3) print(d3) 

輸出結果如下

人生苦短
我用Python
2
1 2
deque([0, 1, '2']) deque([0, 1, '2', 6, 7, 8]) 5 deque(['2', '3', '4', '5']) deque(['2', '3', '4']) deque([1, 2], maxlen=2) deque([2, 3], maxlen=2) 

因此,如果你遇到經常操作列表頭的場景,使用deque最好。deque類的所有方法,自行操作一遍就知道了。

class deque(object): """ deque([iterable[, maxlen]]) --> deque object A list-like sequence optimized for data accesses near its endpoints. """ def append(self, *args, **kwargs): # real signature unknown """ Add an element to the right side of the deque. """ pass def appendleft(self, *args, **kwargs): # real signature unknown """ Add an element to the left side of the deque. """ pass def clear(self, *args, **kwargs): # real signature unknown """ Remove all elements from the deque. """ pass def copy(self, *args, **kwargs): # real signature unknown """ Return a shallow copy of a deque. """ pass def count(self, value): # real signature unknown; restored from __doc__ """ D.count(value) -> integer -- return number of occurrences of value """ return 0 def extend(self, *args, **kwargs): # real signature unknown """ Extend the right side of the deque with elements from the iterable """ pass def extendleft(self, *args, **kwargs): # real signature unknown """ Extend the left side of the deque with elements from the iterable """ pass def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__ """ D.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present. """ return 0 def insert(self, index, p_object): # real signature unknown; restored from __doc__ """ D.insert(index, object) -- insert object before index """ pass def pop(self, *args, **kwargs): # real signature unknown """ Remove and return the rightmost element. """ pass def popleft(self, *args, **kwargs): # real signature unknown """ Remove and return the leftmost element. """ pass def remove(self, value): # real signature unknown; restored from __doc__ """ D.remove(value) -- remove first occurrence of value. """ pass def reverse(self): # real signature unknown; restored from __doc__ """ D.reverse() -- reverse *IN PLACE* """ pass def rotate(self, *args, **kwargs): # real signature unknown """ Rotate the deque n steps to the right (default n=1). If n is negative, rotates left. """ pass 

這裡提示一下,有些函式對佇列進行操作,但返回值是None,比如reverse()反轉佇列,rotate(1)將佇列中元素向右移1位,尾部的元素移到頭部。

二、defaultdict

用途:帶有預設值的字典。父類為Python內建的dict

字典帶預設值有啥好處?舉個栗子,一般來講,建立一個多值對映字典是很簡單的。但是,如果你選擇自己實現的話, 那麼對於值的初始化可能會有點麻煩,你可能會像下面這樣來實現:

d = {}
for key, value in pairs: if key not in d: d[key] = [] d[key].append(value) 

如果使用 defaultdict 的話程式碼就更加簡潔了:

d = defaultdict(list)
for key, value in pairs: d[key].append(value) 

defaultdict 的一個特徵是它會自動初始化每個 key 剛開始對應的值,所以你只需要 關注新增元素操作了。比如:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = 'liao gao xiang'

# 字典中的鍵對映多個值
from collections import defaultdict d = defaultdict(list) print(d) d['a'].append([1, 2, 3]) d['b'].append(2) d['c'].append(3) print(d) d = defaultdict(set) print(d) d['a'].add(1) d['a'].add(2) d['b'].add(4) print(d) 

輸出結果如下:

defaultdict(<class 'list'>, {}) defaultdict(<class 'list'>, {'a': [[1, 2, 3]], 'b': [2], 'c': [3]}) defaultdict(<class 'set'>, {}) defaultdict(<class 'set'>, {'a': {1, 2}, 'b': {4}}) 

三、namedtuple()

用途:建立命名欄位的元組。工廠函式

namedtuple主要用來產生可以使用名稱來訪問元素的資料物件,通常用來增強程式碼的可讀性, 在訪問一些tuple型別的資料時尤其好用。

比如我們使用者擁有一個這樣的資料結構,每一個物件是擁有三個元素的tuple。使用namedtuple方法就可以方便的通過tuple來生成可讀性更高也更好用的資料結構。

from collections import namedtuple

websites = [
    ('Sohu', 'http://www.sohu.com/', u'張朝陽'), ('Sina', 'http://www.sina.com.cn/', u'王志東'), ('163', 'http://www.163.com/', u'丁磊') ] Website = namedtuple('Website', ['name', 'url', 'founder']) for website in websites: website = Website._make(website) print website # 輸出結果: Website(name='Sohu', url='http://www.sohu.com/', founder=u'\u5f20\u671d\u9633') Website(name='Sina', url='http://www.sina.com.cn/', founder=u'\u738b\u5fd7\u4e1c') Website(name='163', url='http://www.163.com/', founder=u'\u4e01\u78ca') 

注意,namedtuple是函式,不是類。

四、Counter

用途:統計可雜湊的物件。父類為Python內建的dict

尋找序列中出現次數最多的元素。假設你有一個單詞列表並且想找出哪個單詞出現頻率最高:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = 'liao gao xiang'

from collections import Counter words = [ 'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes', 'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around', 'the', 'eyes', "don't", 'look', 'around', 'the', 'eyes', 'look', 'into', 'my', 'eyes', "you're", 'under' ] word_counts = Counter(words) # 出現頻率最高的三個單詞 top_three = word_counts.most_common(3) print(top_three) # Outputs [('eyes', 8), ('the', 5), ('look', 4)] print(word_counts['eyes']) morewords = ['why', 'are', 'you', 'not', 'looking', 'in', 'my', 'eyes'] # 如果你想手動增加計數,可以簡單的用加法: for word in morewords: print(word) word_counts[word] += 1 print(word_counts['eyes']) 

結果如下:

[('eyes', 8), ('the', 5), ('look', 4)] 8 why are you not looking in my eyes 9 

因為Counter繼承自dict,所有dict有的方法它都有(defaultdict和OrderedDict也是的),Counter自己實現或重寫了6個方法:

  • most_common(self, n=None),
  • elements(self)
  • fromkeys(cls, iterable, v=None)
  • update(*args, **kwds)
  • subtract(*args, **kwds)
  • copy(self)

五、OrderedDict

用途:排序的欄位。父類為Python內建的dict

OrderedDict在迭代操作的時候會保持元素被插入時的順序,OrderedDict內部維護著一個根據鍵插入順序排序的雙向連結串列。每次當一個新的元素插入進來的時候,它會被放到連結串列的尾部。對於一個已經存在的鍵的重複賦值不會改變鍵的順序。

需要注意的是,一個OrderedDict的大小是一個普通字典的兩倍,因為它內部維護著另外一個連結串列。 所以如果你要構建一個需要大量OrderedDict 例項的資料結構的時候(比如讀取100,000行CSV資料到一個 OrderedDict 列表中去),那麼你就得仔細權衡一下是否使用 OrderedDict帶來的好處要大過額外記憶體消耗的影響。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = 'liao gao xiang'

from collections import OrderedDict d = OrderedDict() d['foo'] = 1 d['bar'] = 2 d['spam'] = 3 d['grok'] = 4 # d['bar'] = 22 #對於一個已經存在的鍵,重複賦值不會改變鍵的順序 for key in d: print(key, d[key]) print(d) import json print(json.dumps(d)) 

結果如下:

foo 1
bar 2
spam 3
grok 4
OrderedDict([('foo', 1), ('bar', 2), ('spam', 3), ('grok', 4)]) {"foo": 1, "bar": 2, "spam": 3, "grok": 4} 

OrderDict實現或重寫了如下方法。都是幹嘛的?這個留給大家當課後作業了_

  • clear(self)
  • popitem(self, last=True)
  • move_to_end(self, key, last=True)
  • keys(self)
  • items(self)
  • values(self)
  • pop(self, key, default=__marker)
  • setdefault(self, key, default=None)
  • copy(self)
  • fromkeys(cls, iterable, value=None)

六、ChainMap

用途:建立多個可迭代物件的集合。類字典型別

很簡單,如下:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = 'liao gao xiang'

from collections import ChainMap from itertools import chain # 不同集合上元素的迭代 a = [1, 2, 3, 4] b = ('x', 'y', 'z') c = {1, 'a'} # 方法一,使用chain for i in chain(a, b, c): print(i) print('--------------') # 方法二,使用chainmap for j in ChainMap(a, b, c): print(j) # 這兩種均為節省記憶體,效率更高的迭代方式 電動叉車 

一個 ChainMap 接受多個字典並將它們在邏輯上變為一個字典。然後,這些字典並不是真的合併在一起了,ChainMap 類只是在內部建立了一個容納這些字典的列表並重新定義了一些常見的字典操作來遍歷這個列表。大部分字典操作都是可以正常使用的,比如:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = 'liao gao xiang'

# 合併多個字典和對映
a = {'x': 1, 'z': 3} b = {'y': 2, 'z': 4} # 現在假設你必須在兩個字典中執行查詢操作 # (比如先從 a 中找,如果找不到再在 b 中找)。 # 一個非常簡單的解決方案就是使用collections模組中的ChainMap類 from collections import ChainMap c = ChainMap(a, b) print(c) a['x'] = 11 # 使用ChainMap時,原字典做了更新,這種更新會合併到新的字典中去 print(c) # 按順序合併兩個字典 print(c['x']) print(c['y']) print(c['z']) # 對於字典的更新或刪除操作影響的總是列中的第一個字典。 c['z'] = 10 c['w'] = 40 del c['x'] print(a) # del c['y']將出現報錯 # ChainMap對於程式語言中的作用範圍變數(比如globals,locals等) # 是非常有用的。事實上,有一些方法可以使它變得簡單: values = ChainMap() # 預設會建立一個空字典 print('\t', values) values['x'] = 1 values = values.new_child() # 新增一個空字典 values['x'] = 2 values = values.new_child() values['x'] = 30 # values = values.new_child() print(values, values['x']) # values['x']輸出最後一次新增的值 values = values.parents # 刪除上一次新增的字典 print(values['x']) values = values.parents print(values) a = {'x': 1, 'y': 2} b = {'y': 2, 'z': 3} merge = dict(b) merge.update(a) print(merge['x'], merge['y'], merge['z']) a['x'] = 11 print(merge['x']) 

輸出結果如下:

ChainMap({'x': 1, 'z': 3}, {'y': 2, 'z': 4}) ChainMap({'x': 11, 'z': 3}, {'y': 2, 'z': 4}) 11 2 3 {'z': 10, 'w': 40} ChainMap({}) ChainMap({'x': 30}, {'x': 2}, {'x': 1}) 30 2 ChainMap({'x': 1}) 1 2 3 1 

作為ChainMap的替代,你可能會考慮使用 update() 方法將兩個字典合併。這樣也能行得通,但是它需要你建立一個完全不同的字典物件(或者是破壞現有字典結構)。同時,如果原字典做了更新,這種改變不會反應到新的合併字典中去。

ChainMap實現或重寫了如下方法:

  • get(self, key, default=None)
  • fromkeys(cls, iterable, *args)