Python 内建模块 之 collections
collections是Python内建的一个集合模块,提供了许多有用的集合类,是对一些基本数据类型的加工
计数器 Counter 。
Counter 是对字典类型的补充,用于追踪值(字符串的字符,或列表元素)的出现次数,具备字典的所有功能+自己的功能
创建计数器:
c = collections.Counter('abcdeabcdabcaba') # count elements from a string
计数器常用操作方法:
help(collections.Counter) class Counter(builtins.dict) | Dict subclass for counting hashable items. Sometimes called a bag | or multiset. Elements are stored as dictionary keys and their counts | are stored as dictionary values. >>> collections.Counter('abbccc') Counter({'a': 1, 'b': 2, 'c': 3}) >>> cnt.items() dict_items([('a', 1), ('b', 2), ('c', 3)]) >>> cnt.keys() dict_keys(['a', 'b', 'c']) >>> cnt.values() dict_values([1, 2, 3]) | most_common(self, n=None) ''' 按次数降序排列 ''' | List the n most common elements and their counts from the most | common to the least. If n is None, then list all element counts. >>> cnt=collections.Counter('accbbbeeee') >>> cnt.most_common(3) [('e', 4), ('b', 3), ('c', 2)] | subtract(*args, **kwds) ''' 更新元素的次数,相减,可以是负数''' | Like dict.update() but subtracts counts instead of replacing them. | Counts can be reduced below zero. >>> cnt=collections.Counter('abbccc') >>> print(cnt) Counter({'c': 3, 'b': 2, 'a': 1}) >>> cnt.subtract('abe') >>> print(cnt) Counter({'c': 3, 'b': 1, 'a': 0, 'e': -1}) | update(*args, **kwds) ''' 更新元素的次数,相加''' | Like dict.update() but add counts instead of replacing them. >>> cnt=collections.Counter('abbccc') >>> print(cnt) Counter({'c': 3, 'b': 2, 'a': 1}) >>> cnt.update('abe') >>> print(cnt) Counter({'b': 3, 'c': 3, 'a': 2, 'e': 1})
有序字典 OrderDict
内部通过列表维护key, 因为列表是有序的,所以实现字典有序。
OrderedDict的Key会按照插入的顺序排列,不是Key本身排序。
创建有序字典:
import collections
ordc=collections.OrderedDict({'k1': 'v1', 'k2': 'v2', 'k3': 'v3'})
有序字典常用方法:和字典几乎相同
常用操作方法:
help(collections.OrderedDict)
class OrderedDict(builtins.dict)
| Dictionary that remembers insertion order
class OrderedDict(builtins.dict) | Dictionary that remembers insertion order | clear(...) | od.clear() -> None. Remove all items from od. | copy(...) | od.copy() -> a shallow copy of od | fromkeys(...) from builtins.type | OD.fromkeys(S[, v]) -> New ordered dictionary with keys from S. | If not specified, the value defaults to None. | items(...) | D.items() -> a set-like object providing a view on D's items | keys(...) | D.keys() -> a set-like object providing a view on D's keys | move_to_end(...) | Move an existing element to the end (or beginning if last==False). Raises KeyError if the element does not exist. | pop(...) | od.pop(k[,d]) -> v, remove specified key and return the corresponding value. If key is not found, d is returned if given, otherwise KeyError is raised. | popitem(...) | od.popitem() -> (k, v), return and remove a (key, value) pair. | Pairs are returned in LIFO order if last is true or FIFO order if false. | setdefault(...) | od.setdefault(k[,d]) -> od.get(k,d), also set od[k]=d if k not in od | update(...) | D.update([E, ]**F) -> None. Update D from dict/iterable E and F. | values(...) | D.values() -> an object providing a view on D's values
比较字典和有序字典,似乎看不到效果(理想的效果普通字典应该每次打印的顺序不一样):
#!/usr/bin/env python
import collections
dc1=dict()
dc1['k1']='v1'
dc1['k2']='v2'
dc1['k3']='v3'
dc2=collections.OrderedDict()
dc2['k1']='v1'
dc2['k2']='v2'
dc2['k3']='v3'
for i in range(5):
print(dc1)
print(dc2)
print('\n')
默认字典 defaultdict
创建默认字典:
import collections
dc=collections.defaultdict(list)
help(collections.defaultdict) class defaultdict(builtins.dict) | defaultdict(default_factory[, ...]) --> dict with default factory >>> dc=collections.defaultdict(list) >>> dc['k1'].append('a') ''' k1开始并不存在,用defaultdict不会抛异常 ''' >>> print(dc) defaultdict(<class 'list'>, {'k1': ['a']}) >>> dc['k2']='b' >>> print(dc) defaultdict(<class 'list'>, {'k1': ['a'], 'k2': 'b'})
可命名元组 namedtuple
为元组的元素命令(标签)
import collections
python 没有提供namedtuple类,自己创建一个namedtuple类:
mytupleClass=collections.namedtuple('mytuple',['x','y','z'])
>>> mytp=mytupleClass(1,2,3)
>>> print(mytp,type(mytp))
mytuple(x=1, y=2, z=3) <class '__main__.mytuple'>
>>> print(mytp.x,mytp.y,mytp.z) # 不需要根据索引打印x,y,z轴的值
1 2 3
help(mytuple) namedtuple(typename, field_names, *, verbose=False, rename=False, module=None) Returns a new subclass of tuple with named fields. | x | Alias for field number 0 | y | Alias for field number 1 | z | Alias for field number 2 | _asdict(self) | Return a new OrderedDict which maps field names to their values. | _replace(_self, **kwds) | Return a new mytuple object replacing specified fields with new values | count(...) | T.count(value) -> integer -- return number of occurrences of value | index(...) | T.index(value, [start, [stop]]) -> integer -- return first index of value. | Raises ValueError if the value is not present.
双向队列 deque
一个线程安全的双向队列
创建队列:
import collections
q=collections.deque('abc',5)
>>> print(q)
deque(['a', 'b', 'c'], maxlen=5)
>>> q.append('d')
>>> print(q)
deque(['a', 'b', 'c', 'd'], maxlen=5)
>>> q.append('e')
>>> print(q)
deque(['a', 'b', 'c', 'd', 'e'], maxlen=5)
>>> q.append('f')
>>> print(q) # 'a' 被挤出
deque(['b', 'c', 'd', 'e', 'f'], maxlen=5)
help(collections.deque) class deque(builtins.object) | deque([iterable[, maxlen]]) --> deque object | append(...) | Add an element to the right side of the deque. | appendleft(...) | Add an element to the left side of the deque. | clear(...) | Remove all elements from the deque. | copy(...) | Return a shallow copy of a deque. | count(...) | D.count(value) -> integer -- return number of occurrences of value | extend(...) | Extend the right side of the deque with elements from the iterable | extendleft(...) | Extend the left side of the deque with elements from the iterable | index(...) | D.index(value, [start, [stop]]) -> integer -- return first index of value. | Raises ValueError if the value is not present. | insert(...) | D.insert(index, object) -- insert object before index | pop(...) | Remove and return the rightmost element. | popleft(...) | Remove and return the leftmost element. | remove(...) | D.remove(value) -- remove first occurrence of value. | reverse(...) | D.reverse() -- reverse *IN PLACE* | rotate(...) ''' 旋转 ''' | Rotate the deque n steps to the right (default n=1). If n is negative, rotates left.
>>> q=collections.deque('abcd') >>> q.rotate(2) >>> print(q) deque(['c', 'd', 'a', 'b'])
单项队列 Queue
既有双向队列,亦有单向队列。Queue不属于collection模块,在此与deque对比
单向队列先进先出FIFO (栈:弹夹,后进先出)
创建单向队列:
import queue # 导入queue模块
q=queue.Queue(3)
单向队列常用操作方法:
help(queue.Queue) class Queue(builtins.object) | Create a queue object with a given maximum size. | If maxsize is <= 0, the queue size is infinite. ''' 如果maxsize小于等于0,这个队列无穷大 ''' | empty(self) | Return True if the queue is empty, False otherwise (not reliable!). | full(self) | Return True if the queue is full, False otherwise (not reliable!). | get(self, block=True, timeout=None) ''' 将元素取出队列 ''' | Remove and return an item from the queue. | get_nowait(self) | Remove and return an item from the queue without blocking. | join(self) | Blocks until all items in the Queue have been gotten and processed. | put(self, item, block=True, timeout=None) ''' 将元素放入队列 ''' | Put an item into the queue. | qsize(self) | Return the approximate size of the queue (not reliable!). | task_done(self) | Indicate that a formerly enqueued task is complete. >>> q=queue.Queue(3) >>> q.put('a') >>> q.put('b') >>> q.put('c') >>> q.qsize() 3 >>> q.get() 'a'

浙公网安备 33010602011771号