python标准库之函数式编程(4)

collections 模块包含了除list、dict、和tuple之外的容器数据类型,如counter、deque、defaultdict、orderdict、namedtuple。

一、Counter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from collections import Counter

print(collections.Counter('abcbcaccbbad'))
print(collections.Counter(['a', 'b', 'c', 'a', 'b', 'b']))
print(collections.Counter({'a':2, 'b':3, 'c':1}))
print(collections.Counter(a=2, b=3, c=1))

# Counter实例支持聚合结果的算术和集合操作
c1 = collections.Counter(['a', 'b', 'c', 'a', 'b', 'b'])
c2 = collections.Counter('alphabet')
print(c1)
print(c2)

print('Combined counts:')
print(c1 + c2)

print('Subtraction counts:')
print(c1 - c2)

print('Intersection(相同键的值取最小):')
print(c1 & c2)

print('Union(相同键的值取最大):')
print(c1 | c2)

1、update()

采用update()方法对容器进行更新

1
2
3
4
5
6
7
8
9
10
11
12
import collections

c = collections.Counter()
print(c)

c.update('abcdaab')
print(c)
# Counter({'a': 3, 'b': 2, 'c': 1, 'd': 1})

c.update({'a':1, 'd':5})
print(c)
# Counter({'d': 6, 'a': 4, 'b': 2, 'c': 1})

2、elements()

elements()方法可以返回一个包含所有Counter数据的迭代器

1
2
3
4
5
6
7
8
9
10
11
12
import collections

c = collections.Counter('abcdaabz')
print(c)
# Counter({'a': 3, 'b': 2, 'c': 1, 'd': 1, 'z': 1})

c['z'] = 0
print(c)
# Counter({'a': 3, 'b': 2, 'c': 1, 'd': 1, 'z': 0})

print(list(c.elements()))
# ['a', 'a', 'a', 'b', 'b', 'c', 'd']

3、most_common()

most_common()返回前n个最多的数据

1
2
3
4
5
import collections

c=collections.Counter('aassdddffff')
for letter, count in c.most_common(2):
print('%s: %d' % (letter, count))

二、deque

双端队列,支持从两端添加和删除元素。更常用的栈和队列是退化形式的双端队列,仅限于一端在输入和输出。

1、两端插入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import collections

# 右端插入
d = collections.deque()
d.extend('abcdefg')
print('extend:', d)
d.append('h')
print('append:', d)

# 左端插入
d = collections.deque()
d.extendleft('abcdefg')
print('extendleft:', d)
d.appendleft('h')
print('appendleft:', d)

2、两端获取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import collections

print('From the right:')
d = collections.deque('abcdefg')
while True:
try:
print(d.pop())
except IndexError:
break

print('From the left:')
d = collections.deque('abcdefg')
while True:
try:
print(d.popleft())
except IndexError:
break

3、双端移动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import collections

d = collections.deque(range(10))
print('Normal:', d)
# deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

d = collections.deque(range(10))
d.rotate(2)
print('Right Move:', d)
# deque([8, 9, 0, 1, 2, 3, 4, 5, 6, 7])

d = collections.deque(range(10))
d.rotate(-2)
print('Left Move:', d)
# deque([2, 3, 4, 5, 6, 7, 8, 9, 0, 1])

三、defaultdict

defaultdict 叫默认字典,是字典的一个子类,继承字典的方法和属性。defaultdict的作用是在于,当字典里的key不存在但被查找时,返回的不是keyError而是一个默认值。

1
2
3
4
5
6
7
8
9
10
11
from collections import defaultdict

dict1 = defaultdict(int)
dict2 = defaultdict(set)
dict3 = defaultdict(str)
dict4 = defaultdict(list)

print(dict1[1])
print(dict2[1])
print(dict3[1])
print(dict4[1])

四、OrderDict

OrderDict 叫有序字典,也是字典类型的一个子类,是对字典的一个补充。
字典类型是一个无序的集合,如果要想将一个传统的字典类型进行排序一般会将字典的键值取出来做排序后在根据键值来进行有序的输出。如果我们定义一个有序字典将不用再如此麻烦,字典顺序将按照录入顺序进行排序且不会改变。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 定义传统字典
dic1 = dict()
# 按顺序添加字典内容
dic1['a'] = '123'
dic1['b'] = 'jjj'
dic1['c'] = '394'
dic1['d'] = '999'
# 排序
dic1_key_list = []
for k in dic1.keys():
dic1_key_list.append(k)
dic1_key_list.sort()
for key in dic1_key_list:
print('dic1字典排序结果 %s:%s' % (key, dic1[key]))

# 定义有序字典
from collections import OrderedDict

dic2 = OrderedDict()
dic2['a'] = '123'
dic2['b'] = 'jjj'
dic2['c'] = 'abc'
dic2['d'] = '999'
for k, v in dic2.items():
print('有序字典:%s:%s' % (k,v))

五、nametuple

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
bob = ('Bob', 30, 'male')
print(bob)

jane = ('Jane', 29, 'female')
print(jane[0])

for p in [bob, jane]:
print('%s is a %d year old %s' % p)

####################################################

from collections import namedtuple

# 创建一个nametuplede 类, 类名称为Person
P = namedtuple('Person', 'name, age, gender')

# 通过Person类实例化一个对象bob
bob = P(name='Bob', age=30, gender='male')

# 通过Person类实例化一个对象jane
jane = P(name='Jane', age=29, gender='female')
print(jane.name)

for p in [bob, jane]:
print('%s is a %d year old %s' % p)