Collections 模塊
知識點
- Counter 類
- defaultdict 類
- namedtuple 類
在這個實驗我們會學習 Collections 模塊。這個模塊實現了一些很好的數據結構,它們能幫助你解決各種實際問題。
>>> import collections
這是如何導入這個模塊,現在我們來看看其中的一些類。
1. Counter
Counter 是一個有助於 hashable 對象計數的 dict 子類。它是一個無序的集合,其中 hashable 對象的元素存儲為字典的鍵,它們的計數存儲為字典的值,計數可以為任意整數,包括零和負數。
我們可以這樣查看 Counter 的幫助信息,事實上這些信息來源於 Counter 的文檔字符串(collections.Counter.__doc__)。


下面我們來看一個例子,例子中我們查看 Python 的 LICENSE 文件中某些單詞出現的次數。
1.1. Counter 示例
>>> from collections import Counter >>> import re >>> path = '/usr/lib/python3.4/LICENSE.txt' >>> words = re.findall('\w+', open(path).read().lower()) >>> Counter(words).most_common(10) [('the', 80), ('or', 78), ('1', 66), ('of', 61), ('to', 50), ('and', 48), ('python', 46), ('in', 38), ('license', 37), ('any', 37)]
Counter 對象有一個叫做 elements() 的方法,其返回的序列中,依照計數重復元素相同次數,元素順序是無序的。
>>> c = Counter(a=4, b=2, c=0, d=-2) >>> list(c.elements()) ['a', 'a', 'a', 'a', 'b', 'b']
most_common() 方法返回最常見的元素及其計數,順序為最常見到最少。
>>> Counter('abracadabra').most_common(3) [('a', 5), ('r', 2), ('b', 2)]
2. defaultdict
defaultdict 是內建 dict 類的子類,它覆寫了一個方法並添加了一個可寫的實例變量。其余功能與字典相同。
defaultdict() 第一個參數提供了 default_factory 屬性的初始值,默認值為 None,default_factory 屬性值將作為字典的默認數據類型。所有剩余的參數與字典的構造方法相同,包括關鍵字參數。
同樣的功能使用 defaultdict 比使用 dict.setdefault 方法快。
defaultdict 用例
>>> from collections import defaultdict >>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)] >>> d = defaultdict(list) >>> for k, v in s: ... d[k].append(v) ... >>> d.items() dict_items([('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])])
在例子中你可以看到,即使 defaultdict 對象不存在某個鍵,它會自動創建一個空列表。
3. namedtuple
命名元組有助於對元組每個位置賦予意義,並且讓我們的代碼有更好的可讀性和自文檔性。你可以在任何使用元組地方使用命名元組。在例子中我們會創建一個命名元組以展示為元組每個位置保存信息。
>>> from collections import namedtuple >>> Point = namedtuple('Point', ['x', 'y']) # 定義命名元組 >>> p = Point(10, y=20) # 創建一個對象 >>> p Point(x=10, y=20) >>> p.x + p.y 30 >>> p[0] + p[1] # 像普通元組那樣訪問元素 30 >>> x, y = p # 元組拆封 >>> x 10 >>> y 20
總結
這個實驗我們使用了 Collections 中的一些數據結構,可能你目前並用不上他,但希望你以后需要的時候會想起它們 : -)
知識點
- Counter 類
- defaultdict 類
- namedtuple 類
在這個實驗我們會學習 Collections 模塊。這個模塊實現了一些很好的數據結構,它們能幫助你解決各種實際問題。
>>> import collections
這是如何導入這個模塊,現在我們來看看其中的一些類。
1. Counter
Counter 是一個有助於 hashable 對象計數的 dict 子類。它是一個無序的集合,其中 hashable 對象的元素存儲為字典的鍵,它們的計數存儲為字典的值,計數可以為任意整數,包括零和負數。
我們可以這樣查看 Counter 的幫助信息,事實上這些信息來源於 Counter 的文檔字符串(collections.Counter.__doc__)。


下面我們來看一個例子,例子中我們查看 Python 的 LICENSE 文件中某些單詞出現的次數。
1.1. Counter 示例
>>> from collections import Counter >>> import re >>> path = '/usr/lib/python3.4/LICENSE.txt' >>> words = re.findall('\w+', open(path).read().lower()) >>> Counter(words).most_common(10) [('the', 80), ('or', 78), ('1', 66), ('of', 61), ('to', 50), ('and', 48), ('python', 46), ('in', 38), ('license', 37), ('any', 37)]
Counter 對象有一個叫做 elements() 的方法,其返回的序列中,依照計數重復元素相同次數,元素順序是無序的。
>>> c = Counter(a=4, b=2, c=0, d=-2) >>> list(c.elements()) ['a', 'a', 'a', 'a', 'b', 'b']
most_common() 方法返回最常見的元素及其計數,順序為最常見到最少。
>>> Counter('abracadabra').most_common(3) [('a', 5), ('r', 2), ('b', 2)]
2. defaultdict
defaultdict 是內建 dict 類的子類,它覆寫了一個方法並添加了一個可寫的實例變量。其余功能與字典相同。
defaultdict() 第一個參數提供了 default_factory 屬性的初始值,默認值為 None,default_factory 屬性值將作為字典的默認數據類型。所有剩余的參數與字典的構造方法相同,包括關鍵字參數。
同樣的功能使用 defaultdict 比使用 dict.setdefault 方法快。
defaultdict 用例
>>> from collections import defaultdict >>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)] >>> d = defaultdict(list) >>> for k, v in s: ... d[k].append(v) ... >>> d.items() dict_items([('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])])
在例子中你可以看到,即使 defaultdict 對象不存在某個鍵,它會自動創建一個空列表。
3. namedtuple
命名元組有助於對元組每個位置賦予意義,並且讓我們的代碼有更好的可讀性和自文檔性。你可以在任何使用元組地方使用命名元組。在例子中我們會創建一個命名元組以展示為元組每個位置保存信息。
>>> from collections import namedtuple >>> Point = namedtuple('Point', ['x', 'y']) # 定義命名元組 >>> p = Point(10, y=20) # 創建一個對象 >>> p Point(x=10, y=20) >>> p.x + p.y 30 >>> p[0] + p[1] # 像普通元組那樣訪問元素 30 >>> x, y = p # 元組拆封 >>> x 10 >>> y 20
總結
這個實驗我們使用了 Collections 中的一些數據結構,可能你目前並用不上他,但希望你以后需要的時候會想起它們 : -)
