Python -> (10) Study Notes

Collection Module

collections Python is a collection of built-in module that provides many useful collections.

>>> import collections

Counter class

CounterIt is a help to the object count hashable dict subclass . It is an unordered collection of elements wherein the storage object is hashable dictionary key, stores the count value thereof dictionary, count may be any integer, including zero and negative.
Counter Help
Counter example : Hits certain word appears in Python LICENSE file.

>>> from collections import Counter
>>> import re
>>> path = '/usr/lib/python3.5/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)]

Object Counter elements()method, which returns the sequence, counting the number of repeated following the same element, the element order is disordered.

>>> c = Counter(a=4, b=2, c=0, d=-2)
>>> list(c.elements())
['b','b','a', 'a', 'a', 'a']

most_common()Returns the most common elements and their count, the order of most common to least

>>> Counter('abracadabra').most_common(3)
[('a', 5), ('r', 2), ('b', 2)]

defaultdict class

defaultdictIt is built dictsubclass of the class which overrides a method and adds a writable instance variable. The remaining functions of the same dictionary.
Using the same function defaultdictthan dict.setdefaultfaster method.

>>> 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])])

Can see that even defaultdict objects of a key does not exist, it will automatically create an empty list.

namedtuple class

Named tuple tuple helps give meaning to each location, and make the code more readable and self-documenting nature.

Create a named tuple to show for the save location information for each tuple:

>>> 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

Source: laboratory building

Published 33 original articles · won praise 1 · views 1241

Guess you like

Origin blog.csdn.net/weixin_44783002/article/details/104638770