Python其他数据结构collection模块-namtuple defaultdict deque Queue Counter OrderDict
Posted 北门吹雪
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python其他数据结构collection模块-namtuple defaultdict deque Queue Counter OrderDict相关的知识,希望对你有一定的参考价值。
nametuple
是tuple扩展子类,命名元组,其实本质上简单类对象
from collections import namedtuple
info = namedtuple("Info", [‘name‘, ‘age‘, ‘height‘])
# 赋值,是不是有点像面向对象中实例变量方式
info.name = "北门吹雪"
info.age = 18
info.height = 175
# 访问
print(info.name)
其实本质上和下面方式一样
class Info:
def __init__(self):
self.name = None
self.age = None
self.height = None
pass
info = Info()
# 赋值
info.name = "北门吹雪"
info.age = 18
info.height = 175
# 访问
print(info.name)
相关方法
1. _make 初始化赋值, 必须长度一致
from collections import namedtuple
info = namedtuple("Info", [‘name‘, ‘age‘, ‘height‘])._make(["北门吹雪", 18, 175])
# 访问
print(info.name)
2. _asdict 将nametuple对象转换为字典对象,是个有序字典
from collections import namedtuple
info = namedtuple("Info", [‘name‘, ‘age‘, ‘height‘])._make(["北门吹雪", 18, 175])
# 访问
print(info._asdict())
defaultdict
是dict的扩展类,访问字典的key如果没有则自动设置默认值,并添加进字典
info = dict()
name = info.setdefault(‘name‘, "北门吹雪")
print(name, info)
from collections import defaultdict
# 默认值必须是可迭代对象
info = defaultdict(lambda: "北门吹雪")
name = info[‘name‘]
print(name, info)
deque
双端队列, 操作和list类似
list deque 推荐用来保存相同类似数据,相关方法和list一致
特性: deque是线程安全的,list不是线程安全,多线程编程则使用deque
from collections import deque
names = deque()
names.append("北门吹雪")
names.append("QiNiuYun")
names.insert(0, "今日头条")
print(names)
Queue
队列(先进先出),通过 deque实现
核心两个方法 put get,会堵塞
from queue import Queue
message = Queue()
# 放入数据
message.put("北门吹雪")
# 消费数据
print(message.get())
Counter
对可迭代对象做统计出现个数,直接返回统计结果,是dict的子类
from collections import Counter
from random import randint
numbers = [randint(1, 5) for _ in range(20)]
numbers_count = Counter(numbers)
print(numbers_count)
相关方法
1. update 添加新的数据
from collections import Counter
from random import randint
numbers = [randint(1, 5) for _ in range(20)]
numbers_count = Counter(numbers)
print(numbers_count)
# 添加新的数据
numbers_count.update([randint(1, 10) for _ in range(20)])
print(numbers_count)
2. most_common(N) 输出出现次数当前最多的前N个元素
from collections import Counter
from random import randint
numbers = [randint(1, 5) for _ in range(20)]
numbers_count = Counter(numbers)
print(numbers_count)
# 添加新的数据
numbers_count.update([randint(1, 10) for _ in range(20)])
print(numbers_count)
# 输出出现次数当前最多的前3个元素,返回列表
print(numbers_count.most_common(3))
OrderDict
继承dict, 保持字典添加顺序,具有dict所有方法
from collections import OrderedDict
info = OrderedDict()
# 填入数据
info["name"] = "北门吹雪"
info[‘age‘] = 18
info[‘height‘] = 175
print(info)
其他方法
1. popitem 默认删除最后的key:value,并返回
from collections import OrderedDict
info = OrderedDict()
# 填入数据
info["name"] = "北门吹雪"
info[‘age‘] = 18
info[‘height‘] = 175
# 返回元组形式
print(info.popitem(‘name‘))
2. pop 必须传入key,删除key:value,返回value
from collections import OrderedDict
info = OrderedDict()
# 填入数据
info["name"] = "北门吹雪"
info[‘age‘] = 18
info[‘height‘] = 175
# 返回age对应的值
print(info.pop(‘age‘))
3. move_to_end 传入key,将元素移到最后
from collections import OrderedDict
info = OrderedDict()
# 填入数据
info["name"] = "北门吹雪"
info[‘age‘] = 18
info[‘height‘] = 175
# 移动数据
info.move_to_end(‘age‘)
print(info)
经验:
1. 这些数据类型基础还是从list tuple set dict基本数据类型扩展而来,本质上添加了一些特性
以上是关于Python其他数据结构collection模块-namtuple defaultdict deque Queue Counter OrderDict的主要内容,如果未能解决你的问题,请参考以下文章