nametuple
是tuple扩展子类,命名元组,其实本质上简单类对象
from collections import namedtupleinfo = namedtuple("Info", ['name', 'age', 'height'])# 赋值,是不是有点像面向对象中实例变量方式info.name = "北门吹雪"info.age = 18info.height = 175# 访问print(info.name)
其实本质上和下面方式一样
class Info: def __init__(self): self.name = None self.age = None self.height = None passinfo = Info()# 赋值info.name = "北门吹雪"info.age = 18info.height = 175# 访问print(info.name)
相关方法
1. _make 初始化赋值, 必须长度一致
from collections import namedtupleinfo = namedtuple("Info", ['name', 'age', 'height'])._make(["北门吹雪", 18, 175])# 访问print(info.name)
2. _asdict 将nametuple对象转换为字典对象,是个有序字典
from collections import namedtupleinfo = 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 dequenames = deque()names.append("北门吹雪")names.append("QiNiuYun")names.insert(0, "今日头条")print(names)
Queue
队列(先进先出),通过 deque实现
核心两个方法 put get,会堵塞
from queue import Queuemessage = Queue()# 放入数据message.put("北门吹雪")# 消费数据print(message.get())
Counter
对可迭代对象做统计出现个数,直接返回统计结果,是dict的子类
from collections import Counterfrom random import randintnumbers = [randint(1, 5) for _ in range(20)]numbers_count = Counter(numbers)print(numbers_count)
相关方法
1. update 添加新的数据
from collections import Counterfrom random import randintnumbers = [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 Counterfrom random import randintnumbers = [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 OrderedDictinfo = OrderedDict()# 填入数据info["name"] = "北门吹雪"info['age'] = 18info['height'] = 175print(info)
其他方法
1. popitem 默认删除最后的key:value,并返回
from collections import OrderedDictinfo = OrderedDict()# 填入数据info["name"] = "北门吹雪"info['age'] = 18info['height'] = 175# 返回元组形式print(info.popitem('name'))
2. pop 必须传入key,删除key:value,返回value
from collections import OrderedDictinfo = OrderedDict()# 填入数据info["name"] = "北门吹雪"info['age'] = 18info['height'] = 175# 返回age对应的值print(info.pop('age'))
3. move_to_end 传入key,将元素移到最后
from collections import OrderedDictinfo = OrderedDict()# 填入数据info["name"] = "北门吹雪"info['age'] = 18info['height'] = 175# 移动数据info.move_to_end('age')print(info)
arrary
只能存放一种类型的数组,这个数组高性能非常高,非常类似list除了只能存放一种类型数据
#标识符 存放数据类型 占用字节
'u' Unicode字符 2
“L” 符号整数 4
“L” 无符号整数 4
“q” 符号整数 8
“q” 无符号整数 8
F ′浮点 4
“D” 浮点 8
import arraynames = array.array("u")print(names.append("北"))print(names.append("门"))print(names.append("吹"))print(names.append("雪"))print(names)print(names[1])
经验:
1. 这些数据类型基础还是从list tuple set dict基本数据类型扩展而来,本质上添加了一些特性
2. 不同的情况下选择不同的数据结构对数据进行处理
北门吹雪: https://www.cnblogs.com/2bjiujiu/