标签:
Counter(计数器)
是一个字典的子类,存储形式同样为字典,其中存储的键为字典的元素,值为元素出现的次数,在使用之前我们需要先导入文件 import collections
import collections # 初始化一个计数器 c = collections.Counter(‘sldfjoaoaufdlfuaof‘) print(c) # Counter({‘f‘: 4, ‘o‘: 3, ‘a‘: 3, ‘d‘: 2, ‘u‘: 2, ‘l‘: 2, ‘j‘: 1, ‘s‘: 1})
import collections # most_common(self,n) 取出计数器总元素最多的前n项 c = collections.Counter(‘sldfjoaoaufdlfuaof‘) d = c.most_common(3) print(d) # [(‘f‘, 4), (‘a‘, 3), (‘o‘, 3)]
import collections # sorted(c) 给计数器排序 c = collections.Counter(‘sldfjoaoaufdlfuaof‘) print(sorted(c)) # [‘a‘, ‘d‘, ‘f‘, ‘j‘, ‘l‘, ‘o‘, ‘s‘, ‘u‘]
import collections # ‘‘.join(sorted(c.elements())) 相当于排序,打印成字符串 # c.elements() 返回所有元素 c = collections.Counter(‘sldfjoaoaufdlfuaof‘) e = ‘‘.join(c) print(e) # ldfjosau f = ‘‘.join(sorted(c.elements())) print(f) # aaaddffffjllooosuu
import collections # sum(c.values()) 所有值个数 c = collections.Counter(‘sldfjoaoaufdlfuaof‘) g = sum(c.values()) print(g) # 18
import collections # c[‘a‘] 字符a出现的次数 c = collections.Counter(‘sldfjoaoaufdlfuaof‘) r = c[‘a‘] print(r) # 3 for elem in ‘shazam‘: c[elem] += 1 d = c[‘a‘] print(d) # 5 del c[‘a‘] print(c) # Counter({‘f‘: 4, ‘o‘: 3, ‘l‘: 2, ‘u‘: 2, ‘s‘: 2, ‘d‘: 2, ‘z‘: 1, ‘m‘: 1, ‘h‘: 1, ‘j‘: 1})
import collections
# update 更新计数器 c = collections.Counter(‘sldfjoaoaufdlfuaof‘) d = collections.Counter(‘aaaaasimsalabim‘) c.update(d) print(c[‘a‘]) # 10
import collections # clear() c = collections.Counter(‘sldfjoaoaufdlfuaof‘) c.clear() print(c) # Counter()
import collections # subtract(*args, **kwds):删除与另一个计数器的交集 c = collections.Counter(‘which‘) c.subtract(‘witch‘) print(c) # Counter({‘h‘: 1, ‘w‘: 0, ‘c‘: 0, ‘i‘: 0, ‘t‘: -1})
OrderDict(有序字典)
OrderedDict 有序字典,是对字典的补充,他能记录字典的添加顺序,相当于字典和列表的组合,使用之前需要导入依赖类 import collections
import collections # 初始化一个有序字典 order = collections.OrderedDict() print(order) # OrderedDict() order[‘k1‘] = ‘v1‘ order[‘k2‘] = ‘v2‘ order[‘k3‘] = ‘v3‘ print(order) # OrderedDict([(‘k1‘, ‘v1‘), (‘k2‘, ‘v2‘), (‘k3‘, ‘v3‘)])
import collections # 初始化一个有序字典 order = collections.OrderedDict() print(order) # OrderedDict() order[‘k1‘] = ‘v1‘ order[‘k2‘] = ‘v2‘ order[‘k3‘] = ‘v3‘ print(order) # OrderedDict([(‘k1‘, ‘v1‘), (‘k2‘, ‘v2‘), (‘k3‘, ‘v3‘)]) # fromkeys(cls, S, v=None) s:定义一个键列表,用于字典的键准备准备的值的列表, v:表示将键设置的值 默认是None s = [‘v1‘,‘k2‘,‘43‘] order1 = order.fromkeys(s,‘Hello‘) print(order1) # OrderedDict([(‘k1‘, ‘Hello‘), (‘k2‘, ‘Hello‘), (‘k3‘, ‘Hello‘)])
import collections # 初始化一个有序字典 order = collections.OrderedDict() print(order) # OrderedDict() order[‘k1‘] = ‘v1‘ order[‘k2‘] = ‘v2‘ order[‘k3‘] = ‘v3‘ print(order) # OrderedDict([(‘k1‘, ‘v1‘), (‘k2‘, ‘v2‘), (‘k3‘, ‘v3‘)]) # items(self, *args, **kwargs) 所有的元素 order2 = order.items() print(order2) # odict_items([(‘k1‘, ‘v1‘), (‘k2‘, ‘v2‘), (‘k3‘, ‘v3‘)]
import collections # 初始化一个有序字典 order = collections.OrderedDict() print(order) # OrderedDict() order[‘k1‘] = ‘v1‘ order[‘k2‘] = ‘v2‘ order[‘k3‘] = ‘v3‘ print(order) # OrderedDict([(‘k1‘, ‘v1‘), (‘k2‘, ‘v2‘), (‘k3‘, ‘v3‘)]) # keys(self, *args, **kwargs) 所有的key order3 = order.keys() print(order3) # odict_keys([‘k1‘, ‘k2‘, ‘k3‘])
import collections # 初始化一个有序字典 order = collections.OrderedDict() print(order) # OrderedDict() order[‘k1‘] = ‘v1‘ order[‘k2‘] = ‘v2‘ order[‘k3‘] = ‘v3‘ print(order) # OrderedDict([(‘k1‘, ‘v1‘), (‘k2‘, ‘v2‘), (‘k3‘, ‘v3‘)]) # move_to_end(self, *args, **kwargs) 将指定元素移动到最后,如果指定元素不存在就会报错 order.move_to_end(‘k1‘) print(order) # OrderedDict([(‘k2‘, ‘v2‘), (‘k3‘, ‘v3‘), (‘k1‘, ‘v1‘)])
import collections # 初始化一个有序字典 order = collections.OrderedDict() print(order) # OrderedDict() order[‘k1‘] = ‘v1‘ order[‘k2‘] = ‘v2‘ order[‘k3‘] = ‘v3‘ print(order) # OrderedDict([(‘k1‘, ‘v1‘), (‘k2‘, ‘v2‘), (‘k3‘, ‘v3‘)]) # pop(self, k, d=None) 弹出指定元素,同时可以接受弹出元素的值 order4 = order.pop(‘k2‘) print(order4) # v2 print(order) # OrderedDict([(‘k3‘, ‘v3‘), (‘k1‘, ‘v1‘)])
# 初始化一个有序字典 order = collections.OrderedDict() print(order) # OrderedDict() order[‘k1‘] = ‘v1‘ order[‘k2‘] = ‘v2‘ order[‘k3‘] = ‘v3‘ print(order) # OrderedDict([(‘k1‘, ‘v1‘), (‘k2‘, ‘v2‘), (‘k3‘, ‘v3‘)]) # popitem(self) 弹出字典中键值对,可以定义一个变量用来接收弹出的建制度,貌似弹出的建制度不是按照顺序弹出来的 order5 = order.popitem() print(order) # OrderedDict([(‘k3‘, ‘v3‘)]) print(order5) # (‘k1‘, ‘v1‘)
import collections order_default = collections.OrderedDict() order_default[‘k100‘] = ‘034‘ order_default.setdefault(‘k1‘,[‘hello‘,‘Python‘]) print(order_default[‘k1‘]) # [‘hello‘, ‘Python‘] print(order_default[‘k100‘]) # 034
import collections order = collections.OrderedDict() order[‘k1‘] = ‘v1‘ order[‘k2‘] = ‘v2‘ order[‘k3‘] = ‘v3‘ order_default.update(order) print(order_default) # OrderedDict([(‘k100‘, ‘034‘), (‘k1‘, ‘v1‘), (‘k2‘, ‘v2‘), (‘k3‘, ‘v3‘)])
import collections order = collections.OrderedDict() order[‘k1‘] = ‘v1‘ order[‘k2‘] = ‘v2‘ order[‘k3‘] = ‘v3‘ order_default.update(order) print(order_default) # OrderedDict([(‘k100‘, ‘034‘), (‘k1‘, ‘v1‘), (‘k2‘, ‘v2‘), (‘k3‘, ‘v3‘)]) # values(self, *args, **kwargs) 所有的值 print(order_default.values()) # odict_values([‘034‘, ‘v1‘, ‘v2‘, ‘v3‘])
# 默认字典 defaultdict 表示其值默认为一个类型 my_defaulf = collections.defaultdict(list) my_defaulf[‘k1‘].append(‘v1‘) print(my_defaulf) # defaultdict(<class ‘list‘>, {‘k1‘: [‘v1‘]})
# 可命名元祖 ,可命名元祖是属于collections类 Mytuple = collections.namedtuple(‘Mytuple‘,[‘name‘, ‘age‘, ‘email‘]) f = Mytuple(‘wang‘,‘12‘,‘wang@163.com‘) print(f.name) # wang print(f.age) # 12 print(f.email) # wang@163.com
双向队列,相当于一个列表,可以从队列的两端来操作,同样在使用之前我们需要先导入 import collections
init_deque = collections.deque() print(init_deque) # deque([])
import collections init_deque = collections.deque() print(init_deque) # deque([]) # append(self, *args, **kwargs) 添加元素 init_deque.append(‘sdfasd‘) print(init_deque) # deque([‘sdfasd‘]) 一次只能添加一个元素 init_deque.append(1) init_deque.append(2) init_deque.append(3) print(init_deque) # deque([‘sdfasd‘, 1, 2, 3]) 默认是向后添加元素
init_deque.appendleft(4) print(init_deque) # deque([4, ‘sdfasd‘, 1, 2, 3])
init_count = init_deque.count(4) print(init_deque) # deque([4, ‘sdfasd‘, 1, 2, 3]) print(init_count) # 1
init_deque.extend(‘100‘) print(init_deque) # deque([4, ‘sdfasd‘, 1, 2, 3, ‘1‘, ‘0‘, ‘0‘])
init_deque.extendleft(‘789‘) print(init_deque) # deque([‘9‘, ‘8‘, ‘7‘, 4, ‘sdfasd‘, 1, 2, 3, ‘1‘, ‘0‘, ‘0‘])
init_index = init_deque.index(1) print(init_index) # 5
init_deque.insert(5,‘567‘) print(init_deque) # deque([‘9‘, ‘8‘, ‘7‘, 4, ‘sdfasd‘, ‘567‘, 1, 2, 3, ‘1‘, ‘0‘, ‘0‘])
print(init_deque) # deque([‘9‘, ‘8‘, ‘7‘, 4, ‘sdfasd‘, ‘567‘, 1, 2, 3, ‘1‘, ‘0‘, ‘0‘]) init_pop = init_deque.pop() print(init_deque) # deque([‘9‘, ‘8‘, ‘7‘, 4, ‘sdfasd‘, ‘567‘, 1, 2, 3, ‘1‘, ‘0‘]) print(init_pop) # 0
print(init_deque) # deque([‘9‘, ‘8‘, ‘7‘, 4, ‘sdfasd‘, ‘567‘, 1, 2, 3, ‘1‘, ‘0‘]) init_popleft = init_deque.popleft() print(init_deque) # deque([‘8‘, ‘7‘, 4, ‘sdfasd‘, ‘567‘, 1, 2, 3, ‘1‘, ‘0‘]) print(init_popleft) # 9
print(init_deque) # deque([‘8‘, ‘7‘, 4, ‘sdfasd‘, ‘567‘, 1, 2, 3, ‘1‘, ‘0‘]) init_deque.remove(1) print(init_deque) # deque([‘8‘, ‘7‘, 4, ‘sdfasd‘, ‘567‘, 2, 3, ‘1‘, ‘0‘])
print(init_deque) # deque([‘8‘, ‘7‘, 4, ‘sdfasd‘, ‘567‘, 2, 3, ‘1‘, ‘0‘]) init_deque.reverse() print(init_deque) # deque([‘0‘, ‘1‘, 3, 2, ‘567‘, ‘sdfasd‘, 4, ‘7‘, ‘8‘])
print(init_deque) # deque([‘0‘, ‘1‘, 3, 2, ‘567‘, ‘sdfasd‘, 4, ‘7‘, ‘8‘]) init_deque.rotate(‘567‘) print(init_deque) # deque([‘0‘, ‘1‘, 3, 2, ‘567‘, ‘sdfasd‘, 4, ‘7‘, ‘8‘])
单项队列,只能在一个方向上操作,先进先出策略,使用时导入 import queue
import queue # 单项队列,只能在一个方向上操作,先进先出原则 init_queue = queue.Queue() print(init_queue) # <queue.Queue object at 0x101379128> 怎么好像是内存地址呢
init_queue.put(‘a‘) print(init_queue) # <queue.Queue object at 0x101b79748> init_queue.put(‘b‘) init_queue.put(‘c‘)
# qsize(self) 队列总元素个数 print(init_queue.qsize()) # 3
# empty(self) 如果返回时Ture 说明是空,其他情况是False,不确定一定有元素 init_empty = init_queue.empty() print(init_empty) # False
# get(self, block=True, timeout=None) 往外拿元素,相当于remove init_geta = init_queue.get() print(init_geta) # a print(init_queue.qsize()) # 2
标签:
原文地址:http://www.cnblogs.com/wang-com/p/5869457.html