赞
踩
- from collections import Counter
- a = ['apple','banana','apple','cat','cat','cat','dog']
- b = Counter(a)
- print('type(b) = ',type(b))
- print('b = ',b)
运行结果:
用普通方法也可以,具体如下:
- a = ['apple','banana','apple','cat','cat','cat','dog']
- b = {}
- for x in a:
- b[x] = b.get(x,0) + 1
- print(b)
运行结果:
可以看出,用标准库Counter更方便一些。
- from collections import Counter
- a = Counter() # 空Counter
- print(a)
- a = Counter('hello world') # 统计每个字符出现次数
- print(a)
- a = Counter([1,2,3,1,2,4]) # 统计每个元素出现的次数
- print(a)
- # 利用字典初始化每个元素(key)和出现的次数(value)
- a = Counter(a = 1,b = 2,c = 3)
- print(a)
- a = Counter({'a':1,'b':2,'c':3})
- print(a)
运行结果:
- from collections import Counter
- a = Counter('hello world')
- print('a = ',a)
-
- print('Top 2 :',a.most_common(2))
- print('Top 1 :',a.most_common(1))
-
- print('a.elements = ',a.elements())
- print('all elements = ',list(a.elements()))
-
- print(a.keys())
- print(a.values())
-
- a.clear()
- print('a = ',a)
-
- c = Counter(a = 3,b = 2)
- d = Counter(a = 2,b = 1)
- print('c + d = ',c + d)
- print('c - d = ',c - d)
- print('c & d = ',c & d)
- print('c | d = ',c | d)
运行结果:
append(x) : 添加x到右端
appendleft(x) : 添加x到左端
pop() : 移去并且返回一个元素,deque最右端的那个
popleft() : 移去并且返回一个元素,deque最左端的那个
insert(i,x) : 在位置 i 插入 x
extend(iterable) : 扩展deque的右侧,通过添加 iterable 参数中的元素
extend(iterable) : 扩展deque的左侧,通过添加 iterable 参数中的元素
注意,左添加时,在结果中 iterable 参数中的顺序将被反过来添加
remove(value) : 移除找到的第一个 value
- from collections import deque
- a = deque([1,2,3,4])
- a.append(5)
- a.appendleft(0)
- print('a = ',a)
-
- a.pop()
- print('a = ',a)
- a.popleft()
- print('a = ',a)
-
- a.extend([2,3,5])
- print('a = ',a)
-
- a.remove(2)
- print('a = ',a)
运行结果:
clear() : 清空
copy():拷贝
count(x):计算deque中x的个数
index(x,[,start[,stop]]): 返回x在deque中的位置(索引start之后,索引stop之前)
reverse():将deque逆序排列
rotate(n) :向右循环移动n步,若n为负数,则向左移动n步
maxlen: deque的最大尺寸,如果没有限制就是None
- from collections import deque
- a = deque([1,2,3,1,4,5,2,6,2,6])
- print('a = ',a)
-
- a.reverse()
- print('a = ',a)
-
- b = a.count(2)
- print('2出现的次数是 :',b)
-
- a.rotate(2)
- print('a = ',a)
-
- print('4的位置是:',a.index(4))
运行结果:
- from collections import defaultdict
- d = defaultdict(int)
- print(d['x'])
- d = defaultdict(list)
- print(d['x'])
- d = defaultdict(dict)
- print(d['x'])
- d = defaultdict(set)
- print(d['x'])
运行结果:
- from collections import defaultdict
- d = defaultdict(int)
- print(d['x'])
- d = defaultdict(list)
- print(d['x'])
- d = defaultdict(dict)
- print(d['x'])
- d = defaultdict(set)
- print(d['x'])
运行结果:
- from collections import defaultdict
- a = [('reda',1),('yellow',2),('black',3),('yellow',4),('blue',5)]
- d = {}
- for k,v in a:
- if k not in d.keys():
- d[k] = []
- d[k].append(v)
- print(d)
运行结果:
举例:(按顺序删)
- from collections import OrderedDict
- a = [('a',1),('b',2),('c',3)]
- d = OrderedDict(a)
- print(d)
- while len(d) != 0:
- print('删除的元素为:',d.popitem(False))
- print('d = ',d)
运行结果:
(逆序删)
- from collections import OrderedDict
- a = [('a',1),('b',2),('c',3)]
- d = OrderedDict(a)
- print(d)
- while len(d) != 0:
- print('删除的元素为:',d.popitem(True))
- print('d = ',d)
运行结果:
普通方法:
- from collections import OrderedDict
- a = [('a',1),('b',2),('c',3)]
- d = dict(a)
- print(d)
- while len(d) != 0:
- print('删除的元素为:',d.popitem())
- print('d = ',d)
运行结果:
OrderDict将key移动到任一端,last = True表示右端,否则就是左端。key不存在则报错。
- from collections import OrderedDict
- a = [('a',1),('b',2),('c',3)]
- d = OrderedDict(a)
- print(d)
- d.move_to_end('b')
- print(d)
- d.move_to_end('b',False)
- print(d)
运行结果:
OK,又复习完了一个竞赛常用标准库,明天继续!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。