Python基礎(chǔ)(4)- collections

昨天用到了這個(gè)collections模塊,挺好用的,這里記錄下。
官網(wǎng)介紹:https://docs.python.org/3/library/collections.html
博客:廖雪峰的博客
這里介紹些好玩兒的例子。

namedtuple

collections.namedtuple(typename, field_names, *, verbose=False, rename=False, module=None)
Returns a new tuple subclass named typename. The new subclass is used to create tuple-like objects that have fields accessible by attribute lookup as well as being indexable and iterable. Instances of the subclass also have a helpful docstring (with typename and field_names) and a helpful repr() method which lists the tuple contents in a name=value format.

namedtuple是一個(gè)工廠函數(shù),返回一個(gè)自定義的tuple類,可讀性更強(qiáng)些。
通常我們使用tuple的時(shí)候,像這樣

point_a = 1,3

point_b = 2,6

point_a
Out[37]: (1, 3)

point_b
Out[38]: (2, 6)

point_a[0]
Out[39]: 1

point_a[1]
Out[40]: 3

我們是那個(gè)namedtuple就可以這樣了

from collections import namedtuple

Point = namedtuple('Point',['x','y'])

point_a = Point(2,2)

point_b = Point(3,3)

point_a
Out[45]: Point(x=2, y=2)

point_b
Out[46]: Point(x=3, y=3)

point_a.x
Out[47]: 2

point_b.y
Out[48]: 3

這樣使用一個(gè)坐標(biāo)位置,是不是可讀性更強(qiáng)呢,而且用起來(lái)也很方便
我們可以看看這個(gè)Point是怎樣定義的

print(point_a._source)
from builtins import property as _property, tuple as _tuple
from operator import itemgetter as _itemgetter
from collections import OrderedDict

class Point(tuple):
    'Point(x, y)'

    __slots__ = ()

    _fields = ('x', 'y')

    def __new__(_cls, x, y):
        'Create new instance of Point(x, y)'
        return _tuple.__new__(_cls, (x, y))

    @classmethod
    def _make(cls, iterable, new=tuple.__new__, len=len):
        'Make a new Point object from a sequence or iterable'
        result = new(cls, iterable)
        if len(result) != 2:
            raise TypeError('Expected 2 arguments, got %d' % len(result))
        return result

    def _replace(_self, **kwds):
        'Return a new Point object replacing specified fields with new values'
        result = _self._make(map(kwds.pop, ('x', 'y'), _self))
        if kwds:
            raise ValueError('Got unexpected field names: %r' % list(kwds))
        return result

    def __repr__(self):
        'Return a nicely formatted representation string'
        return self.__class__.__name__ + '(x=%r, y=%r)' % self

    def _asdict(self):
        'Return a new OrderedDict which maps field names to their values.'
        return OrderedDict(zip(self._fields, self))

    def __getnewargs__(self):
        'Return self as a plain tuple.  Used by copy and pickle.'
        return tuple(self)

    x = _property(_itemgetter(0), doc='Alias for field number 0')

    y = _property(_itemgetter(1), doc='Alias for field number 1')

下面還有個(gè)更好用的地方,我們?cè)僮x取CSV或者數(shù)據(jù)庫(kù)的時(shí)候,會(huì)返回結(jié)果集,這個(gè)時(shí)候用起來(lái)更方便,比如:

import csv
from collections import namedtuple
       
EmployeeRecord = namedtuple('EmployeeRecord', 'name, age, title, department, paygrade')
for emp in map(EmployeeRecord._make, csv.reader(open(r'D:\document\python_demo\employee_data.csv'))):
    print(emp.name, emp.title)
    print('emp:',emp)


runfile('D:/document/python_demo/demo_hi.py', wdir='D:/document/python_demo')
lufei leader
emp: EmployeeRecord(name='lufei', age='20', title='leader', department='onepiece', paygrade='100')
namei teacher
emp: EmployeeRecord(name='namei', age='19', title='teacher', department='onepiece', paygrade='999')

_make

somenamedtuple._make(iterable)
Class method that makes a new instance from an existing sequence or iterable.

deque

我們使用list的時(shí)候,用下標(biāo)查找很快,數(shù)據(jù)量大的時(shí)候,插入刪除比較慢,deque是為了高效實(shí)現(xiàn)插入和刪除的雙向隊(duì)列。

deque:double-ended queue

class collections.deque([iterable[, maxlen]])
Returns a new deque object initialized left-to-right (using append()) with data from iterable. If iterable is not specified, the new deque is empty.

from collections import deque

a = deque(list('abcdef'))

a
Out[80]: deque(['a', 'b', 'c', 'd', 'e', 'f'])

a.append('x')

a.append('y')

a
Out[83]: deque(['a', 'b', 'c', 'd', 'e', 'f', 'x', 'y'])

a.appendleft('w')

a
Out[85]: deque(['w', 'a', 'b', 'c', 'd', 'e', 'f', 'x', 'y'])

a.pop()
Out[86]: 'y'

a.popleft()
Out[87]: 'w'

這里擴(kuò)展了很多方便的函數(shù),appendleft(),popleft()等等

defaultdict

可以設(shè)置默認(rèn)值的dict,平時(shí)我們使用dict的時(shí)候,如果key不存在,會(huì)報(bào)錯(cuò)

class collections.defaultdict([default_factory[, ...]])
Returns a new dictionary-like object. defaultdict is a subclass of the built-in dict class. It overrides one method and adds one writable instance variable. The remaining functionality is the same as for the dict class and is not documented here.

a = {'name':'lufe','age':20}

a
Out[105]: {'age': 20, 'name': 'lufe'}

a['name']
Out[106]: 'lufe'

a['age']
Out[107]: 20

a['score']
Traceback (most recent call last):

  File "<ipython-input-108-99f54e089332>", line 1, in <module>
    a['score']

KeyError: 'score'

我們使用defaultdict就可以避免這個(gè)錯(cuò)誤

from collections import defaultdict

b = defaultdict(int)

b['name']='lufei'

b
Out[123]: defaultdict(int, {'name': 'lufei'})

b['age']
Out[124]: 0

這里我們?cè)O(shè)置默認(rèn)是int型,默認(rèn)值為0

x = defaultdict(0)
Traceback (most recent call last):

  File "<ipython-input-125-dd2052e23af0>", line 1, in <module>
    x = defaultdict(0)

TypeError: first argument must be callable or None


x = defaultdict(lambda : 100)

x
Out[127]: defaultdict(<function __main__.<lambda>>, {})

x['name']
Out[128]: 100

Counter

是一個(gè)簡(jiǎn)單的計(jì)數(shù)器,

class collections.Counter([iterable-or-mapping])
A Counter is a dict subclass for counting hashable objects. It is an unordered collection where elements are stored as dictionary keys and their counts are stored as dictionary values. Counts are allowed to be any integer value including zero or negative counts. The Counter class is similar to bags or multisets in other languages.

from collections import Counter

cnt = Counter(['red', 'blue', 'red', 'green', 'blue', 'blue'])

cnt
Out[131]: Counter({'blue': 3, 'green': 1, 'red': 2})

cnt.most_common(1)
Out[132]: [('blue', 3)]

cnt.most_common(-1)
Out[133]: []

cnt.elements
Out[134]: <bound method Counter.elements of Counter({'blue': 3, 'red': 2, 'green': 1})>

cnt.most_common(3)[:-2:-1]
Out[137]: [('green', 1)]

這個(gè)most_common最好用了感覺(jué),根據(jù)次數(shù)進(jìn)行排名

當(dāng)然,collections中還有很多其他的好用的類,我們可以參考官方文檔。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容