0. 简介
- 核心原理与基础信息
- 常用功能/函数
2.1. namedtuple: 命名一个不变列表,简洁的class用法
2.2 deque 高效增删改双向列表
2.3 defaultdict key不存在时,返回默认值
2.4 OrderedDict 有序词典,就是加了个顺序而已
2.5 Counter 计数
1. 核心原理与基础信息
-
核心:有点像个罐子(叫container),里面可以装
dict
,list
,set
, andtuple
.然后做一些操作。 -
注意有点像 以上四类的一个子集,例如 namedtuple() ∈ tuple() ; 如果 g ∈ namedtuple(), 那么g ∈ namedtuple() ∈ tuple() 。其他类似。
- collections中包含什么函数/功能?
# input
import collections
print(dir(collections))
#output
['AsyncGenerator', 'AsyncIterable', 'AsyncIterator', 'Awaitable', 'ByteString', 'Callable', 'ChainMap', 'Collection', 'Container', 'Coroutine', 'Counter', 'Generator', 'Hashable', 'ItemsView', 'Iterable', 'Iterator', 'KeysView', 'Mapping', 'MappingView', 'MutableMapping', 'MutableSequence', 'MutableSet', 'OrderedDict', 'Reversible', 'Sequence', 'Set', 'Sized', 'UserDict', 'UserList', 'UserString', 'ValuesView', '_Link', '_OrderedDictItemsView', '_OrderedDictKeysView', '_OrderedDictValuesView', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_chain', '_class_template', '_collections_abc', '_count_elements', '_eq', '_field_template', '_heapq', '_iskeyword', '_itemgetter', '_proxy', '_recursive_repr', '_repeat', '_repr_template', '_starmap', '_sys', 'abc', 'defaultdict', 'deque', 'namedtuple']
2. 常用功能
2.1. namedtuple: 命名一个不变列表,简洁的class
用法
- 例如我想定义一个三维坐标(x,y,z),可以给他起个名字P3。
- 调用tuple元素方式不一样,例如调用三维点a中x轴的值,用a.x, 不能用a[0]。
- 这样定义的元素,既属于tuple,又属于P3.
代码1-namedtuple
# input code
#namedtuple in collections
from collections import namedtuple
three_diamention_Point = namedtuple('three_diamention_Point',['x','y','z'])
a = three_diamention_Point(1,2,3)
print('point a is : \n',a)
print('the valus on the x axis is : \n', a.x)
#output
point a is :
three_diamention_Point(x=1, y=2, z=3)
the valus on the x axis is :
1
#check the type
print(isinstance(a,three_diamention_Point))
print(isinstance(a,tuple))
#output
True
True
代码2-make赋值
>>> t = [11, 22]
>>> Point._make(t)
Point(x=11, y=22)
代码3-更改值
>>> p = Point(x=11, y=22)
>>> p._replace(x=33)
Point(x=33, y=22)
代码4-tuple回顾
# input code
b = (1,2,3)
print(type(b))
print(b[0])
#output
<class 'tuple'>
1
2.2 deque 高效增删改双向列表
- 类似
list
,就是在list
外面加个deque()
定义。好处就是增删改比list
快很多。用法差不多。 - 适用于队列和栈,例如
pop
,popleft
# input
from collections import deque
q = deque(['a', 'b', 'c'])
print(dir(deque))
#output
['append', 'appendleft', 'clear', 'copy', 'count', 'extend', 'extendleft',
'index', 'insert', 'maxlen', 'pop', 'popleft', 'remove', 'reverse', 'rotate']
- 反向打印出元素
reversed()
,不对原表做操作。跟list类似
3.1 deque
# IN python3.6
from collections import deque
d = deque(['a', 'b', 'c'])
c = list(reversed(d))
print(c)
print(d)
# OUT
['c', 'b', 'a']
deque(['a', 'b', 'c'])
3.2 list
# IN
a = ('a','b','c')
b = list(reversed(a))
print(a)
print(b)
# OUT
('a', 'b', 'c')
['c', 'b', 'a']
- 其他判断存在与否,一次性插入大量元素等等
>>> 'h' in d # search the deque
True
>>> d.extend('jkl') # add multiple elements at once
>>> d
deque(['g', 'h', 'i', 'j', 'k', 'l'])
>>> d.rotate(1) # right rotation
>>> d
deque(['l', 'g', 'h', 'i', 'j', 'k'])
>>> d.rotate(-1) # left rotation
>>> d
deque(['g', 'h', 'i', 'j', 'k', 'l'])
>>> deque(reversed(d)) # make a new deque in reverse order
deque(['l', 'k', 'j', 'i', 'h', 'g'])
>>> d.clear() # empty the deque
>>> d.pop() # cannot pop from an empty deque
Traceback (most recent call last):
File "<pyshell#6>", line 1, in -toplevel-
d.pop()
IndexError: pop from an empty deque
>>> d.extendleft('abc') # extendleft() reverses the input order
>>> d
deque(['c', 'b', 'a'])
2.3 defaultdict key
不存在时,返回默认值
>>> from collections import defaultdict
>>> dd = defaultdict(lambda: 'N/A')
>>> dd['key1'] = 'abc'
>>> dd['key1'] # key1存在
'abc'
>>> dd['key2'] # key2不存在,返回默认值
'N/A'
2.4 OrderedDict 有序词典,就是加了个顺序而已
不过Python3.6 开始dict也是有序的,我就说我写了N个例子都是有序...
可以用来写 先进先出 的FIFO
2.5 Counter 计数
好方便,一定要记住。用来计数,可以算字符串,数组list,词典dict,等等。想到就能用的功能。最好用的有most_common([n])
- 字符串
# Uses python 3.6 for strings, test 1
from collections import Counter
c = Counter()
for ch in 'shlasdlan':
c[ch] += 1
print(isinstance(c,Counter))
print(isinstance(c,dict))
print(c)
# test 1 output
True
True
Counter({'s': 2, 'l': 2, 'a': 2, 'h': 1, 'd': 1, 'n': 1})
- list 类似
#In
from collections import Counter
cnt = Counter()
for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:
cnt[word] += 1
print(cnt)
#Out
Counter({'blue': 3, 'red': 2, 'green': 1})
- 引用正则,一行就能对整个文件计数
# IN Uses python 3.6
from collections import Counter
import re
words = re.findall(r'\w+',open('mbox-short.txt').read().lower())
print(Counter(words).most_common(3))
# Out
[('edu', 461), ('2008', 380), ('jan', 352)]
- 可以随便写的函数
>>> from collections import Counter
>>> c = Counter('gallahad')
>>> c
Counter({'a': 3, 'l': 2, 'g': 1, 'h': 1, 'd': 1})
>>> c = Counter({'red': 4, 'blue': 2})
>>> c
Counter({'red': 4, 'blue': 2})
>>> c = Counter(cats=4, dogs=8)
>>> c
Counter({'dogs': 8, 'cats': 4})
>>> c = Counter(a=4, b=2, c=0, d=-2)
>>> c
Counter({'a': 4, 'b': 2, 'c': 0, 'd': -2})
- elements() 小于零时不显示
>>> c = Counter(a=4, b=2, c=0, d=-2)
>>> sorted(c.elements())
['a', 'a', 'a', 'a', 'b', 'b']
- Counter()还能加减乘除和逻辑运算
>>> c = Counter(a=4, b=2, c=0, d=-2)
>>> d = Counter(a=1, b=2, c=3, d=4)
>>> c.subtract(d)
>>> c
Counter({'a': 3, 'b': 0, 'c': -3, 'd': -6})
>>> c = Counter(a=3, b=1)
>>> d = Counter(a=1, b=2)
>>> c + d # add two counters together: c[x] + d[x]
Counter({'a': 4, 'b': 3})
>>> c - d # subtract (keeping only positive counts)
Counter({'a': 2})
>>> c & d # intersection: min(c[x], d[x])
Counter({'a': 1, 'b': 1})
>>> c | d # union: max(c[x], d[x])
Counter({'a': 3, 'b': 2})
[2018.5.10]
参考资料:
遗留问题:
chainmap暂时不会用
网友评论