Python——collections模块

collections模块

collections模块在内置数据类型(dict、list、set、tuple)的基础上,还提供了几个额外的数据类型:ChainMap、Counter、deque、defaultdict、namedtuple和OrderedDict等。

1.namedtuple: 生成可以使用名字来访问元素内容的tuple子类
2.deque: 双端队列,可以快速的从另外一侧追加和推出对象
3.Counter: 计数器,主要用来计数
4.OrderedDict: 有序字典
5.defaultdict: 带有默认值的字典

namedtuple

namedtuple是一个函数,它用来创建一个自定义的tuple对象,并且规定了tuple元素的个数,并可以用属性而不是索引来引用tuple的某个元素。

这样一来,我们用namedtuple可以很方便地定义一种数据类型,它具备tuple的不变性,又可以根据属性来引用,使用十分方便。

>>> from collections import namedtuple
>>> Point = namedtuple('Point', ['x', 'y'])
>>> 
>>> p = Point(1,2)
>>> p.x
1
>>> p.y
2
>>> Circle = namedtuple('Circle', ['x', 'y', 'r'])
>>> c = Circle(1,2,3)
>>> c
Circle(x=1, y=2, r=3)
>>> c.x
1
>>> c.y
2
#namedtuple('名称', [属性list]):

可以验证创建的Point对象是tuple的一种子类:

>>> Point = namedtuple('Point',['x1','y1','x2','y2'])
>>> p = Point('1','2','3','4')
>>> p
Point(x1='1', y1='2', x2='3', y2='4')
>>> p.x1
'1'
>>> isinstance(p,Point)
True
>>> isinstance(p,tuple)
True

deque

使用list存储数据时,按索引访问元素很快,但是插入和删除元素就很慢了,因为list是线性存储,数据量大的时候,插入和删除效率很低。

deque是为了高效实现插入和删除操作的双向列表,适合用于队列和栈:

>>> from collections import deque
>>> q = deque(['a', 'b', 'c'])
>>> q.appendleft
<built-in method appendleft of collections.deque object at 0x028F8228>
>>> q.appendleft('1')
>>> q
deque(['1', 'a', 'b', 'c'])
>>> q.append('2')
>>> q
deque(['1', 'a', 'b', 'c', '2'])
>>> q.popleft()
'1'
>>> q
deque(['a', 'b', 'c', '2'])
>>> q.pop()
'2'
>>> q
deque(['a', 'b', 'c'])

deque除了实现list的append()pop()外,还支持appendleft()popleft(),这样就可以非常高效地往头部添加或删除元素。

defaultdict

使用dict时,如果引用的Key不存在,就会抛出KeyError。如果希望key不存在时,返回一个默认值,就可以用defaultdict

deque(['a', 'b', 'c'])
>>> from collections import defaultdict
>>> d = defaultdict(lambda:'N/A')
>>> d
defaultdict(<function <lambda> at 0x02A1F970>, {})
>>> d['key1']=1
>>> d['key1']
1
>>> d['key2']
'N/A'
>>> from collections import defaultdict
>>> d = defaultdict(0)

Traceback (most recent call last):
  File "<pyshell#173>", line 1, in <module>
    d = defaultdict(0)
TypeError: first argument must be callable or None
>>> d = defaultdict(str)
>>> d
defaultdict(<type 'str'>, {})
>>> d['x']
''
>>> d = defaultdict(int)
>>> d['x']
0

注意默认值是调用函数返回的而函数在创建defaultdict对象时传入

除了在Key不存在时返回默认值,defaultdict的其他行为跟dict是完全一样的。

OrderedDict

使用dict时,Key是无序的。在对dict做迭代时,我们无法确定Key的顺序。

如果要保持Key的顺序,可以用OrderedDict

>>> from collections import OrderedDict
>>> d = dict([('a',1),('b',2),('c',3)])
>>> d
{'a': 1, 'c': 3, 'b': 2}
>>> od = OrderedDict([('a',1),('b',2),('c',3)])
>>> od
OrderedDict([('a', 1), ('b', 2), ('c', 3)])

注意,OrderedDict的Key会按照插入的顺序排列,不是Key本身排序:

>>> od = OrderedDict()
>>> od['z'] = 1
>>> od['y'] = 2
>>> od['x'] = 3
>>> od.keys() # 按照插入的Key的顺序返回
['z', 'y', 'x']

OrderedDict可以实现一个FIFO(先进先出)的dict,当容量超出限制时,先删除最早添加的Key:

from collections import OrderedDict

class LastUpdatedOrderedDict(OrderedDict):

    def __init__(self, capacity):
        super(LastUpdatedOrderedDict, self).__init__()
        self._capacity = capacity

    def __setitem__(self, key, value):
        containsKey = 1 if key in self else 0
        if len(self) - containsKey >= self._capacity:
            last = self.popitem(last=False)
            print 'remove:', last
        if containsKey:
            del self[key]
            print 'set:', (key, value)
        else:
            print 'add:', (key, value)
        OrderedDict.__setitem__(self, key, value)

Counter

Counter是一个简单的计数器,例如,统计字符出现的个数:

>>> from collections import Counter
>>> c = Counter()
>>> for ch in 'programming':
...     c[ch] = c[ch] + 1
...
>>> c
Counter({'g': 2, 'm': 2, 'r': 2, 'a': 1, 'i': 1, 'o': 1, 'n': 1, 'p': 1})

Counter实际上也是dict的一个子类,上面的结果可以看出,字符'g''m''r'各出现了两次,其他字符各出现了一次。

案例:

统计一篇英文文章内每个单词出现频率,并返回出现频率最高的前3个单词及其出现次数

txt_content = '''She had been shopping with her Mom in Wal-Mart. 
She must have been 6 years old, this beautiful brown haired, freckle-faced image of innocence. 
It was pouring outside. The kind of rain that gushes over the top of rain gutters, 
so much in a hurry to hit the Earth, it has no time to flow down the spout.
We all stood there under the awning and just inside the door of the Wal-Mart.
We all waited, some patiently, others irritated,
because nature messed up their hurried day. 
I am always mesmerized by rainfall. 
I get lost in the sound and sight of the heavens washing away the dirt and dust of the world. 
Memories of running, splashing so carefree as a child come pouring in as a welcome reprieve from the worries of my day.
'''

 使用传统字典

def str_count(s):
    """
    统计英文单词出现次数
    :param s:
    :return:
    """
    d = dict()
    s = re.split('\W+', s.strip())  # 去掉回车,逗号等非英文和数字字符
    for x in s:
        if x not in d:
            d[x] = 1
        else:
            d[x]+=1
    return d

if __name__ == '__main__':
    d1 = str_count(txt_content)
    print(sorted(d1.items(), key = lambda d:d[1],reverse=True)[:3])

使用defaultdict

from collections import defaultdict
from collections import Counter
import re


def str_count(s):
    """
    统计英文单词出现次数
    :param s:
    :return:
    """
    d = defaultdict(int)
    s = re.split('\W+', s.strip())  # 去掉回车,逗号等非英文和数字字符
    for x in s:
        d[x] += 1
    return d

if __name__ == '__main__':
    d1 = str_count(txt_content)
    print(sorted(d1.items(), key=lambda d: d[1], reverse=True)[:3])

使用counter

from collections import Counter
import re


def str_count(s):
    """
    统计英文单词出现次数
    :param s:
    :return:
    """
    c = Counter()
    s = re.split('\W+', s.strip())  # 去掉回车,逗号等非英文和数字字符
    c.update(s)
    return c

if __name__ == '__main__':
    c = str_count(txt_content)
    print(c.most_common(3))

 

posted @ 2018-04-24 17:43  一只小小的寄居蟹  阅读(415)  评论(0编辑  收藏  举报