Python基础之collection
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python基础之collection相关的知识,希望对你有一定的参考价值。
collection-系列
cellection是作为字典、元组(列表与元组可互相转换)的扩充,在此需要导入cellection
一、计数器(counter)
counter是对字典类型的补充,用户获取字典中元素出现的次数。它具备字典所有的功能以及自己自带的功能。
1 import collections 2 stra = collections.Counter(‘asdfasdfasdfdgghjertgfgsa‘) 3 print(stra) 4 输出的结果是: 5 Counter({‘a‘: 4, ‘d‘: 4, ‘g‘: 4, ‘f‘: 4, ‘s‘: 4, ‘e‘: 1, ‘h‘: 1, ‘j‘: 1, ‘r‘: 1, ‘t‘: 1}) 6 后面的数字表示的是出现的次数。
most_common,出现最多
1 import collections 2 stra = collections.Counter(‘asdfasdfasdf[dggh]jertgfgsa‘) 3 print(stra) 4 ret = stra.most_common(4) 5 print(ret) 6 测试结果: 7 Counter({‘a‘: 4, ‘d‘: 4, ‘g‘: 4, ‘f‘: 4, ‘s‘: 4, ‘e‘: 1, ‘h‘: 1, ‘j‘: 1, ‘r‘: 1, ‘t‘: 1, ‘[‘: 1, ‘]‘: 1}) 8 9 [(‘a‘, 4), (‘d‘, 4), (‘g‘, 4), (‘f‘, 4)] 输出了排名前四的
elements元素:
1 import collections 2 stra = collections.Counter(‘asdfasdfasdf[dggh]jertgfgsa‘) 3 for items in stra.elements(): 4 print(items) 5 执行结果: 6 Counter({‘a‘: 4, ‘d‘: 4, ‘g‘: 4, ‘f‘: 4, ‘s‘: 4, ‘e‘: 1, ‘h‘: 1, ‘j‘: 1, ‘r‘: 1, ‘t‘: 1, ‘[‘: 1, ‘]‘: 1}) 7 a 8 a 9 a 10 a 11 e 12 d 13 d 14 d 15 d 16 g 17 g 18 g 19 g 20 f 21 f 22 f 23 f 24 h 25 j 26 s 27 s 28 s 29 s 30 r 31 t 32 [ 33 ] 34 区别于: 35 import collections 36 stra = collections.Counter(‘asdfasdfasdf[dggh]jertgfgsa‘) 37 print (stra) 38 # for item in stra.elements(): 39 # print(item) 40 for item in stra.items(): 41 print(item) 42 执行结果: 43 Counter({‘a‘: 4, ‘d‘: 4, ‘g‘: 4, ‘f‘: 4, ‘s‘: 4, ‘e‘: 1, ‘h‘: 1, ‘j‘: 1, ‘r‘: 1, ‘t‘: 1, ‘[‘: 1, ‘]‘: 1}) 44 (‘a‘, 4) 45 (‘e‘, 1) 46 (‘d‘, 4) 47 (‘g‘, 4) 48 (‘f‘, 4) 49 (‘h‘, 1) 50 (‘j‘, 1) 51 (‘s‘, 4) 52 (‘r‘, 1) 53 (‘t‘, 1) 54 (‘[‘, 1) 55 (‘]‘, 1)
更新(增加删除)操作:
1 增加: 2 import collections 3 stra = collections.Counter([‘aa‘,‘bb‘,‘cc‘]) 4 print(stra) 5 stra.update([‘dd‘,‘aa‘,‘bb‘]) 6 print(stra) 7 结果: 8 Counter({‘aa‘: 1, ‘cc‘: 1, ‘bb‘: 1}) 9 Counter({‘aa‘: 2, ‘bb‘: 2, ‘cc‘: 1, ‘dd‘: 1}) 证明增加成功。 10 删除: 11 import collections 12 stra = collections.Counter([‘aa‘,‘bb‘,‘cc‘]) 13 print(stra) 14 stra.update([‘dd‘,‘aa‘,‘bb‘]) 15 print(stra) 16 stra.subtract([‘dd‘,‘aa‘,‘bb‘]) 17 print(stra) 18 输出结果: 19 Counter({‘aa‘: 1, ‘cc‘: 1, ‘bb‘: 1}) 20 Counter({‘aa‘: 2, ‘bb‘: 2, ‘cc‘: 1, ‘dd‘: 1}) 21 Counter({‘aa‘: 1, ‘cc‘: 1, ‘bb‘: 1, ‘dd‘: 0})证明已经删除了。
二、有序字典(orderedDict)
有序字典是对字典的补充:
1 dic = collections.OrderedDict() 2 dic[‘k1‘] = ‘v1‘ 3 dic[‘k2‘] = ‘v2‘ 4 dic[‘k3‘] = ‘v3‘ 5 dic[‘k4‘] = ‘v4‘ 6 dic[‘k5‘] = ‘v5‘ 7 print(dic) 8 结果输出: 9 OrderedDict([(‘k1‘, ‘v1‘), (‘k2‘, ‘v2‘), (‘k3‘, ‘v3‘), (‘k4‘, ‘v4‘), (‘k5‘, ‘v5‘)]) 10 值不会变而且是固定了,不像字典一样位置会是浮动变化的。 11 12 popitem用法: 13 dic = collections.OrderedDict() 14 dic[‘k1‘] = ‘v1‘ 15 dic[‘k2‘] = ‘v2‘ 16 dic[‘k3‘] = ‘v3‘ 17 dic[‘k4‘] = ‘v4‘ 18 dic[‘k5‘] = ‘v5‘ 19 print(dic) 20 c = dic.popitem() 21 print(dic) 22 print(c) 23 输出结果: 24 OrderedDict([(‘k1‘, ‘v1‘), (‘k2‘, ‘v2‘), (‘k3‘, ‘v3‘), (‘k4‘, ‘v4‘), (‘k5‘, ‘v5‘)]) 25 OrderedDict([(‘k1‘, ‘v1‘), (‘k2‘, ‘v2‘), (‘k3‘, ‘v3‘), (‘k4‘, ‘v4‘)]) 26 (‘k5‘, ‘v5‘)取出了‘k5’,‘v5‘是最后进的,所以遵循后进先出。 27 pop用法: 28 dic = collections.OrderedDict() 29 dic[‘k1‘] = ‘v1‘ 30 dic[‘k2‘] = ‘v2‘ 31 dic[‘k3‘] = ‘v3‘ 32 dic[‘k4‘] = ‘v4‘ 33 dic[‘k5‘] = ‘v5‘ 34 print(dic) 35 # c = dic.popitem() 36 c = dic.pop(‘k2‘) ###指定取出k2 37 print(dic) 38 print(c) 39 输出结果: 40 OrderedDict([(‘k1‘, ‘v1‘), (‘k2‘, ‘v2‘), (‘k3‘, ‘v3‘), (‘k4‘, ‘v4‘), (‘k5‘, ‘v5‘)]) 41 OrderedDict([(‘k1‘, ‘v1‘), (‘k3‘, ‘v3‘), (‘k4‘, ‘v4‘), (‘k5‘, ‘v5‘)]) pop表示是拿出删除据为己有。 42 v2 得到k2值 43 44 新增update: 45 dic = collections.OrderedDict() 46 dic[‘k1‘] = ‘v1‘ 47 dic[‘k2‘] = ‘v2‘ 48 dic[‘k3‘] = ‘v3‘ 49 dic[‘k4‘] = ‘v4‘ 50 dic[‘k5‘] = ‘v5‘ 51 print(dic) 52 dic.update({‘k1‘:‘12‘,‘k5‘:‘10‘,‘k10‘:‘20‘}) 53 print(dic) 54 输出结果: 55 OrderedDict([(‘k1‘, ‘v1‘), (‘k2‘, ‘v2‘), (‘k3‘, ‘v3‘), (‘k4‘, ‘v4‘), (‘k5‘, ‘v5‘)]) 56 OrderedDict([(‘k1‘, ‘12‘), (‘k2‘, ‘v2‘), (‘k3‘, ‘v3‘), (‘k4‘, ‘v4‘), (‘k5‘, ‘10‘), (‘k10‘, ‘20‘)])表示有k值更新k值。没有就新增。
三、默认字典(defaultdict)
defaultdict是对字典的类型的补充,他默认给字典的值设置了一个类型。
1 import collections 2 dic = collections.defaultdict(list) #定义默认字典类型为list。 3 dic[‘k1‘].append(‘100‘) 4 print(dic) 5 输出: 6 defaultdict(<type ‘list‘>, {‘k1‘: [‘100‘]}) 7 8 如果不采用默认字典: 9 import collections 10 dic = {} 11 dic[‘k1‘].append(‘100‘) 12 print(dic) 13 会报错: 14 Traceback (most recent call last): 15 File "C:/Users/daisy/PycharmProjects/s12/day3/collection-???.py", line 54, in <module> 16 dic[‘k1‘].append(‘100‘) 17 KeyError: ‘k1‘
四、可命名元组(namedtuple)
1 import collections 2 #创建类。MyTupleClass 3 MyTupleClass = collections.namedtuple(‘MyTupleClass‘,[‘x‘,‘y‘,‘z‘]) 4 obj = MyTupleClass(‘xx‘,‘yy‘,‘zz‘) 5 print(obj.x) 6 print(obj.y) 7 print(obj.z) 8 测试: 9 xx 10 yy 11 zz
五、双向队列(deque)
1 que = collections.deque() 2 que.append(‘1‘) 3 que.appendleft(‘10‘) 4 que.append(‘20‘) 5 que.extend([‘xx‘,‘xxd‘,‘xxxd‘]) 6 que.extendleft([‘11‘,‘22‘,‘333‘]) 7 print(que) 8 print(que.rotate(2)) 9 print(que.count(‘1‘)) 10 print(que) 11 执行: 12 deque([‘333‘, ‘22‘, ‘11‘, ‘10‘, ‘1‘, ‘20‘, ‘xx‘, ‘xxd‘, ‘xxxd‘]) 13 None 14 1 15 deque([‘xxd‘, ‘xxxd‘, ‘333‘, ‘22‘, ‘11‘, ‘10‘, ‘1‘, ‘20‘, ‘xx‘])
六、单向队列(queue.queue)在queue
1 import Queue 2 q = Queue.Queue() 3 q.put("abc") 4 q.put("def") 5 print(q.qsize()) 6 print(q.get()) 7 运行结果: 8 9 2 10 abc 先进先出:abc先进所以输出abc。
以上是关于Python基础之collection的主要内容,如果未能解决你的问题,请参考以下文章