计数器(counter),有序字典(OrderDict),默认字典(defaultdict),可命名元祖(namedtuple),双向队列(deque),单项队列(deuqe.Queue)
2016-11-23 16:22
639 查看
Python_Day_05 计数器(counter),有序字典(OrderDict),默认字典(defaultdict),可命名元祖(namedtuple),双向队列(deque),单项队列(deuqe.Queue)
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})
most_common(self,n) 取出元素最多的前n项
import collections # most_common(self,n) 取出计数器总元素最多的前n项 c = collections.Counter('sldfjoaoaufdlfuaof') d = c.most_common(3) print(d) # [('f', 4), ('a', 3), ('o', 3)]
sorted(c) 给计数器排序
import collections # sorted(c) 给计数器排序 c = collections.Counter('sldfjoaoaufdlfuaof') print(sorted(c)) # ['a', 'd', 'f', 'j', 'l', 'o', 's', 'u']
''.join(sorted(c.elements())) 排序,打印成字符串形式
elements(c) 计数器所有元素
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
sum(*args, **kwargs) 增加
sum(c.values()) c字典所有元素个数
import collections # sum(c.values()) 所有值个数 c = collections.Counter('sldfjoaoaufdlfuaof') g = sum(c.values()) print(g) # 18
c['a'] c字典中a元素出现次数
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})
update(*args, **kwds) 更新计数器
import collections # update 更新计数器 c = collections.Counter('sldfjoaoaufdlfuaof') d = collections.Counter('aaaaasimsalabim') c.update(d) print(c['a']) # 10
clear(self) 清除所有参数
import collections # clear() c = collections.Counter('sldfjoaoaufdlfuaof') c.clear() print(c) # Counter()
subtract(*args, **kwds) 删除与另一个交集后的子集
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')])
fromkeys(cls, S, v=None) s:定义一个键列表,用于字典的键准备准备的值的列表, v:表示将键设置的值 默认是None
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')])
items(self, *args, **kwargs) 所有的元素
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')]
keys(self, *args, **kwargs) 所有的key
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'])
move_to_end(self, *args, **kwargs) 将指定元素移动到最后,如果指定元素不存在就会报错
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')])
pop(self, k, d=None) 弹出指定元素,同时可以接受弹出元素的值
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')])
popitem(self) 弹出字典中键值对,可以定义一个变量用来接收弹出的建制度,貌似弹出的建制度不是按照顺序弹出来的
# 初始化一个有序字典 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')
setdefault(self, k, d=None) 用来查找字典中键,如果字典中不存在key就默认为d
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
update(self, *args, **kwargs) 更新
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) 所有的值
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'])
defalultdict(默认字典)
# 默认字典 defaultdict 表示其值默认为一个类型 my_defaulf = collections.defaultdict(list) my_defaulf['k1'].append('v1') print(my_defaulf) # defaultdict(<class 'list'>, {'k1': ['v1']})
namedtuple(可命名元祖)
# 可命名元祖 ,可命名元祖是属于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
deque(双向队列)
双向队列,相当于一个列表,可以从队列的两端来操作,同样在使用之前我们需要先导入 import collections
初始化
init_deque = collections.deque() print(init_deque) # deque([])
append(self, *args, **kwargs) 添加元素
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]) 默认是向后添加元素
appendleft(self, *args, **kwargs) 想左添加元素
init_deque.appendleft(4) print(init_deque) # deque([4, 'sdfasd', 1, 2, 3])
clear(self, *args, **kwargs) 清除
copy(self, *args, **kwargs) 拷贝
count(self, value) 计算指定值(value)的个数
init_count = init_deque.count(4) print(init_deque) # deque([4, 'sdfasd', 1, 2, 3]) print(init_count) # 1
extend(self, *args, **kwargs) 在右边扩展,同时会将传入的参数拆分成元素来添加
init_deque.extend('100') print(init_deque) # deque([4, 'sdfasd', 1, 2, 3, '1', '0', '0'])
extendleft(self, *args, **kwargs) 会将传入的参数拆分成元素来添加到左边,完全倒序,最右边的元素添加到最左边
init_deque.extendleft('789') print(init_deque) # deque(['9', '8', '7', 4, 'sdfasd', 1, 2, 3, '1', '0', '0'])
index(self, value, start=None, stop=None) 指定值在指定范围内的下标 默认是整个队列
init_index = init_deque.index(1) print(init_index) # 5
insert(self, index, p_object) 在指定下标插入指定元素
init_deque.insert(5,'567') print(init_deque) # deque(['9', '8', '7', 4, 'sdfasd', '567', 1, 2, 3, '1', '0', '0'])
pop(self, *args, **kwargs) 从右边弹出一个元素 同时可以接收弹出的元素
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
popleft(self, *args, **kwargs) 弹出最左边的元素
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
remove(self, value) 移除在队列中出现的第一个指定的值
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'])
reverse(self) 队列反向排序
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'])
rotate(self, *args, **kwargs) 将指定元素向前移动n个位置,如果n没有设置就默认移动到左边一个位置,但是n怎么设置还没搞定
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'])
Queue(单项队列)
单项队列,只能在一个方向上操作,先进先出策略,使用时导入 import queue
初始化
import queue # 单项队列,只能在一个方向上操作,先进先出原则 init_queue = queue.Queue() print(init_queue) # <queue.Queue object at 0x101379128> 怎么好像是内存地址呢
put(self, item, block=True, timeout=None) 向队列添加元素
init_queue.put('a') print(init_queue) # <queue.Queue object at 0x101b79748> init_queue.put('b') init_queue.put('c')
qsize(self) 队列总元素个数
# qsize(self) 队列总元素个数 print(init_queue.qsize()) # 3
empty(self) 如果返回时Ture 说明是空,其他情况是False,不确定一定有元素
# empty(self) 如果返回时Ture 说明是空,其他情况是False,不确定一定有元素 init_empty = init_queue.empty() print(init_empty) # False
get(self, block=True, timeout=None) 往外拿元素,相当于remove
# get(self, block=True, timeout=None) 往外拿元素,相当于remove init_geta = init_queue.get() print(init_geta) # a print(init_queue.qsize()) # 2
init_queue_max = queue.Queue(maxsize=3) 最多存放元素个数为3个,多余的put进去的都会处于等待状态
相关文章推荐
- Python_Day_05 计数器(counter),有序字典(OrderDict),默认字典(defaultdict),可命名元祖(namedtuple),双向队列(deque),单项队列(deuqe.Queue)
- day3 collections(counter(对字典的加工) 有序字典(orderedDict)默认字典(defaultdict))可命名元组(namedtuple)
- python-有序字典、默认字典、双向队列、队列
- 有序字典(OrderedDict)、默认字典(defaultdict)内置函数
- Python学习笔记(三十三)常用内置模块(2)collections_namedtuple_deque_defaultdict_OrderedDict_Counter
- collections系列之OrderedDict【有序字典】与DefaultDict【默认字典】
- 【python】collections模块(有序字典,计数器,双向队列)
- python基础-namedtuple、queue队列、deque双向队列
- STL:队列(queue)、优先级队列(priority_queue )及双向队列(deque)总结
- STL之queue队列 stack栈 priority_queue优先队列 deque双向队列
- collections之deque【双向队列】与Queue【单向队列】
- python学习笔记-Day03 -第一部分(collections,Counter,defaultdict,namedtuple)
- python的collection系列-默认字典(defaultdict)
- Python学习笔记-Day03 -第二部分(双向队列-deque和单向队列Queue)
- STL系列之一 deque双向队列
- STL系列之一 deque双向队列
- STL之deque(双向队列)
- deque 双向队列
- Python dict 将元祖转成字典
- python(3)-计数器,有序字典