collections模块基本介绍
collections在通用的容器dict,list,set和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 |
namedtuple()
tuple类似于数组,只能通过下表来访问各个元素。使用namedtuple,每个元素有自己的名字,数据的意义一目了然。
In [22]: from collections import namedtuple In [23]: Point = namedtuple('Point', ['x', 'y']) In [24]: p = Point(11, y=22) In [25]: p[0] + p[1] Out[25]: 33 In [26]: p.x Out[26]: 11 In [27]: p.y Out[27]: 22 In [28]: p Out[28]: Point(x=11, y=22) In [29]: x, y = p In [30]: x Out[30]: 11
命名元组还有三种额外的方法,两个属性
classmethod somenamedtuple.
_make
(iterable)
Class method that makes a new instance from an existing sequence or iterable.
从一个已经存在的序列或可迭代对象创建一个新实例
In [1]: from collections import namedtuple In [2]: Point = namedtuple('Point', ['x', 'y', 'z']) In [3]: t = [1, 2, 3] In [4]: p = Point._make(t) In [5]: p Out[5]: Point(x=1, y=2, z=3)
somenamedtuple.
_asdict
()
Return a new OrderedDict
which maps field names to their corresponding values:
返回一个新的OrderedDict,并以field names作为key, field names对应值作为values。
In [16]: from collections import namedtuple In [17]: Point = namedtuple('Point', ['x', 'y', 'z']) In [18]: t = [1, 2, 3] In [19]: p = Point._make(t) In [20]: p Out[20]: Point(x=1, y=2, z=3) In [21]: d = p._asdict() In [22]: d.get('x') Out[22]: 1 In [23]: d Out[23]: OrderedDict([('x', 1), ('y', 2), ('z', 3)])
somenamedtuple.
_replace
(kwargs)
Return a new instance of the named tuple replacing specified fields with new values:
返回一个用新值替换指定字段后的命名元组的一个新实例。
In [24]: from collections import namedtuple In [25]: Point = namedtuple('Point', ['x', 'y', 'z']) In [26]: t = [1, 2, 3] In [27]: p = Point._make(t) In [28]: p Out[28]: Point(x=1, y=2, z=3) In [29]: p._replace(z=4) Out[29]: Point(x=1, y=2, z=4) In [30]: p.z Out[30]: 3 In [31]: p = p._replace(z=4) In [33]: p.z Out[33]: 4 In [34]: p Out[34]: Point(x=1, y=2, z=4)
somenamedtuple.
_fields
Tuple of strings listing the field names. Useful for introspection and for creating new named tuple types from existing named tuples.
字段名列表
In [35]: p._fields Out[35]: ('x', 'y', 'z')
somenamedtuple.
_source
A string with the pure Python source code used to create the named tuple class. The source makes the named tuple self-documenting. It can be printed, executed using exec()
, or saved to a file and imported.
创建命名元组的纯python代码
In [36]: p._source Out[36]: "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, z)' __slots__ = () _fields = ('x', 'y', 'z') def __new__(_cls, x, y, z): 'Create new instance of Point(x, y, z)' return _tuple.__new__(_cls, (x, y, z)) @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) != 3: raise TypeError('Expected 3 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', 'z'), _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, z=%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') z = _property(_itemgetter(2), doc='Alias for field number 2') "
OrderedDict()
字典都是是无序的,通过OrderedDict创建的字典会记住插入的顺序。
In [40]: from collections import OrderedDict In [41]: hexm = {'name': 'hexiaoming', 'age': 15, 'sexy': 'male'} In [42]: dict(hexm) Out[42]: {'age': 15, 'name': 'hexiaoming', 'sexy': 'male'} In [43]: OrderedDict(hexm) Out[43]: OrderedDict([('name', 'hexiaoming'), ('age', 15), ('sexy', 'male')])
In [44]: d1 = OrderedDict(hexm)
In [45]: d1['eat'] = 'gousi'
In [46]: d1
Out[46]:
OrderedDict([('name', 'hexiaoming'),
('age', 15),
('sexy', 'male'),
('eat', 'gousi')])
对字典排序:
In [47]: d = {'banana': 3, 'apple': 4, 'pear': 1, 'orange': 2} In [48]: OrderedDict(sorted(d.items(), key=lambda t: t[0])) Out[48]: OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)]) In [49]: OrderedDict(sorted(d.items(), key=lambda t: t[1])) Out[49]: OrderedDict([('pear', 1), ('orange', 2), ('banana', 3), ('apple', 4)]) In [50]: OrderedDict(sorted(d.items(), key=lambda t: len(t[0]))) Out[50]: OrderedDict([('pear', 1), ('apple', 4), ('banana', 3), ('orange', 2)])