常用模块之collections模块
collections模块
1.ametuple 具名元组
"""
namedtuple('名称',[名字1,名字2,...])
namedtuple('名称','名字1 名字2 ...')
"""
2.queue 内置队列模块(解释器自带) # 内置队列模块:FIFO
import queue
q = queue.Queue() # 初始化队列
q.put('添加内容') #往队列中添加元素
#从队列中获取元素
print(q.get()) # 值去没了则原地等待
3.deque 双端队列模块
from collections import deque
q = deque([11,22,33])
q.append(44) # 从右边添加
q.appendleft(55) # 从左边添加
print(q.pop()) # 从右边取值
print(q.popleft()) # 从做边取值
4.orderedDict 有序字典
from collections import OrderedDict
order_dict = OrderedDict([('name', 'lili'), ('age', 17)])
order_dict['hobby'] = 'read'
print(order_dict)
5.defaultdict 默认字典
from collections import defaultdict
values = [11, 22, 33,44,55,66,77,88,99,90]
my_dict = defaultdict(list)
for value in values:
if value > 60:
my_dict['k1'].append(value)
else:
my_dict['k2'].append(value)
print(my_dict)
6.counter 计数器
from collections import Counter # 计数器
res = 'hellohello'
res = Counter(res)
# ({'l': 4, 'h': 2, 'e': 2, 'o': 2})
```