虽用的不多,但还是记录一下
python内置模块之collections模块
一、collections模块说明
一个模块主要用来干嘛,有哪些类可以使用,看__init__.py就知道
1、查看collections模块的定义路径
import collections
print(collections.__file__)
'C:\Users\DELL\AppData\Local\Programs\Python\Python38\lib\collections\__init__.py'
2、查看collections文档介绍信息
import collections
print(collections.__doc__)
'''
This module implements specialized container datatypes providing
alternatives to Python's general purpose built-in containers, dict,
list, set, and 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
'''
中文释意:
collections包含了一些特殊的容器,针对Python内置的容器,例如list、dict、set和tuple,提供了另一种选择;
namedtuple,可以创建包含名称的tuple;
deque,类似于list的容器,可以快速的在队列头部和尾部添加、删除元素;
Counter,dict的子类,计算可hash的对象;
OrderedDict,dict的子类,可以记住元素的添加顺序,是一个有序字典;
defaultdict,dict的子类,带有默认值的字典;
3、查看collections的所有可用的类、方法和属性
import collections
print(dir(collections))
['ChainMap', 'Counter', 'OrderedDict', 'UserDict', 'UserList', 'UserString', '_Link', '_OrderedDictItemsView',
'_OrderedDictKeysView', '_OrderedDictValuesView', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
'__getattr__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_chain', '_collections_abc',
'_count_elements', '_eq', '_heapq', '_iskeyword', '_itemgetter', '_proxy', '_recursive_repr', '_repeat', '_starmap',
'_sys', '_tuplegetter', 'abc', 'defaultdict', 'deque', 'namedtuple']
二、collections模块的具体使用
1、namedtuple()函数的使用
namedtuple
生成可以使用名字来访问元素内容的tuple
1、查看一下namedtuple有哪些属性
from collections import namedtuple
print(dir(namedtuple))
['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__',
'__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__',
'__hash__', '__init__', '__init_subclass__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__',
'__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__',
'__subclasshook__']
2、namedtuple的官方文档
>>> from collections import namedtuple
>>> print(namedtuple.__doc__)
Returns a new subclass of tuple with named fields.
>>> Point = namedtuple('Point', ['x', 'y'])
>>> Point.__doc__ # docstring for the new class
'Point(x, y)'
>>> p = Point(11, y=22) # instantiate with positional args or keywords
>>> p[0] + p[1] # indexable like a plain tuple
33
>>> x, y = p # unpack like a regular tuple
>>> x, y
(11, 22)
>>> p.x + p.y # fields also accessible by name
33
>>> d = p._asdict() # convert to a dictionary
>>> d['x']
11
>>> Point(**d) # convert from a dictionary
Point(x=11, y=22)
>>> p._replace(x=100) # _replace() is like str.replace() but targets named fields
3、namedtuple的具体使用举例
定义:namedtuple('名称', [ 属性list]):
下面定义一个圆,可以通过元组对应的名字来访问对应的值,下面访问圆的圆心坐标和半径
>>> from collections import namedtuple
>>> circle = namedtuple("Circle", ["x", "y", "r"])
>>> circle
<class '__main__.Circle'>
>>> circle1 = circle(2,4,6)
>>> circle1
Circle(x=2, y=4, r=6)
>>> circle1.x
2
>>> circle1.y
4
>>> circle1.r
6
2、defaultdict类的具体使用
defaultdict
带有默认值的字典
1、查看defaultdict类的方法和属性
from collections import defaultdict
print(dir(defaultdict))
['__class__', '__contains__', '__copy__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__',
'__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__missing__', '__ne__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__',
'__subclasshook__', 'clear', 'copy', 'default_factory', 'fromkeys', 'get', 'items', 'keys', 'pop',
'popitem', 'setdefault', 'update', 'values']
因为defaultdict是继承了dict类的,所以dict的方法属性defaultdict都可以用,下面列出dict的方法和属性,你和上面的对比一下
>>> dir(dict)
['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__',
'__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__',
'__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop',
'popitem', 'setdefault', 'update', 'values']
2、defaultdict的官方文档
>>> print(defaultdict.__doc__)
defaultdict(default_factory[, ...]) --> dict with default factory
The default factory is called without arguments to produce
a new value when a key is not present, in __getitem__ only.
A defaultdict compares equal to a dict with the same items.
All remaining arguments are treated the same as if they were
passed to the dict constructor, including keyword arguments.
3、Counter类的使用
1、counter类的使用
Counter类的目的是用来跟踪值出现的次数。它是一个无序的容器类型,以字典的键值对形式存储,其中元素作为key,其计数作为value。计数值可以是任意的Interger(包括0和负数)。Counter类和其他语言的bags或multisets很相似。
>>> from collections import Counter
>>> c = Counter('abcdeabcdabcaba')
>>> c
Counter({'a': 5, 'b': 4, 'c': 3, 'd': 2, 'e': 1})
2、Counter对象创建的四种方法
>>> c = Counter() # 创建一个空的Counter类
>>> c = Counter('gallahad') # 从一个可iterable对象(list、tuple、dict、字符串等)创建
>>> c = Counter({'a': 4, 'b': 2}) # 从一个字典对象创建
>>> c = Counter(a=4, b=2) # 从一组键值对创建
正常访问字典,如果键不存在会返回KeyError, 但是Counter不会,它返回的是0,因为它返回的是个数
>>> from collections import Counter
>>> c = Counter('abcdeabcdabcaba')
>>> c
Counter({'a': 5, 'b': 4, 'c': 3, 'd': 2, 'e': 1})
>>> b = dict(c)
>>> b
{'a': 5, 'b': 4, 'c': 3, 'd': 2, 'e': 1}
>>> b["name"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'name'
>>> c['a']
5
>>> c['name']
0