Python其他数据结构collection模块-namtuple defaultdict deque Queue Counter OrderDict arrary
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)
arrary
只能存放一种类型的数组,这个数组高性能非常高,非常类似list除了只能存放一种类型数据
#标识符 存放数据类型 占用字节
'u' Unicode字符 2
“L” 符号整数 4
“L” 无符号整数 4
“q” 符号整数 8
“q” 无符号整数 8
F ′浮点 4
“D” 浮点 8
import array
names = 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/
Python其他数据结构collection模块-namtuple defaultdict deque Queue Counter OrderDict arrary的更多相关文章
- Python高级数据结构-Collections模块
在Python数据类型方法精心整理,不必死记硬背,看看源码一切都有了之中,认识了python基本的数据类型和数据结构,现在认识一个高级的:Collections 这个模块对上面的数据结构做了封装,增加 ...
- Python原生数据结构增强模块collections
collections简介 python提供了4种基本的数据结构:list.tuple.dict.set.基本数据结构完全可以hold住所有的场景,但是在处理数据结构复杂的场景时,这4种数据结构有时会 ...
- Python常用数据结构之collections模块
Python数据结构常用模块:collections.heapq.operator.itertools collections collections是日常工作中的重点.高频模块,常用类型由: 计数器 ...
- Python collection模块与深浅拷贝
collection模块是对Python的通用内置容器:字典.列表.元组和集合的扩展,它包含一些专业的容器数据类型: Counter(计数器):dict子类,用于计算可哈希性对象的个数. Ordere ...
- Python常用数据结构之heapq模块
Python数据结构常用模块:collections.heapq.operator.itertools heapq 堆是一种特殊的树形结构,通常我们所说的堆的数据结构指的是完全二叉树,并且根节点的值小 ...
- python双端队列-collection模块
双端队列(double-ended queue,或者称deque)在需要按照元素增加的顺序来移除元素时非常有用.其中collection模块,包括deque类型. 使用实例:
- Python进阶(十)----软件开发规范, time模块, datatime模块,random模块,collection模块(python额外数据类型)
Python进阶(十)----软件开发规范, time模块, datatime模块,random模块,collection模块(python额外数据类型) 一丶软件开发规范 六个目录: #### 对某 ...
- (python数据分析)第03章 Python的数据结构、函数和文件
本章讨论Python的内置功能,这些功能本书会用到很多.虽然扩展库,比如pandas和Numpy,使处理大数据集很方便,但它们是和Python的内置数据处理工具一同使用的. 我们会从Python最基础 ...
- python-Day3-set 集合-counter计数器-默认字典(defaultdict) -可命名元组(namedtuple)-有序字典(orderedDict)-双向队列(deque)--Queue单项队列--深浅拷贝---函数参数
上节内容回顾:C语言为什么比起他语言块,因为C 会把代码变异成机器码Pyhton 的 .pyc文件是什么python 把.py文件编译成的.pyc文件是Python的字节码, 字符串本质是 字符数组, ...
随机推荐
- go微服务系列(四) - http api中引入protobuf
1. protobuf相关依赖安装 2. 改造之前的client 2.1 新建proto文件 2.2 运行protoc命令生成go文件 2.3 然后把原来的map修改成具体的类型就可以了 3. 处理j ...
- zero:如何找到SEO流量的突破口
http://www.wocaoseo.com/thread-326-1-1.html 之前一篇文章已有提到过,SEO可以分为两个阶段: 一个阶段是了解点SEO知识,然后就到网站上去找页面,看哪些招式 ...
- Serializers组件详解
Serializers组件 使用背景 因为每个语言都有自己的数据类型,不同语言要想数据传输,就必须指定一种各种语言通用的数据类型,如json,xml等等 序列化器允许把像查询集和模型实例这样的复杂数据 ...
- Python Selenium 之数据驱动测试的实现
数据驱动模式的测试好处相比普通模式的测试就显而易见了吧!使用数据驱动的模式,可以根据业务分解测试数据,只需定义变量,使用外部或者自定义的数据使其参数化,从而避免了使用之前测试脚本中固定的数据.可以将测 ...
- 断言函数-RF
测试用例的目的是要验证一些操作否符合我们的预期结果,所以在测试用例中,断言函数是必不可少的一项.我们做的每一步操作都会有预期的结果,为了保证操作得到的结果符合预期,我们需要在测试用例中添加断言,来保证 ...
- ES6--let,解构赋值,promise && ES7--async
ES-->IE10.Google.火狐 ES6 let 声明的关键字 不能重复声明 块级作用域 <input type="button" value="1&q ...
- App性能测试前需要了解的内存原理
这两天在研究性能中内存方面的一块,网上也零散看了挺多文章,写得很细但是感觉不够整体,所以这篇算是总结一下吧,当个复习资料. 那么这里个人分为两个大部分,第一部分应用内的内存管理,主要是oom的理解,G ...
- steam 数据转换
目录 数组和集合互转 数组转集合 方法一 遍历 方法二 asList 方法三 steam 集合转数组 方法一 循环 方法二 toArray 方法三 steam 小结 string转为Character ...
- 04router
1.以 / 开头的嵌套路径会被当作根路径.一级路由可以放在二级router-view里面 实现的效果是页面嵌套 { path: '/console', name: 'console', compone ...
- 外包公司派遣到网易,上班地点网易大厦,转正后工资8k-10k,13薪,包三餐,值得去吗?
作为一个人,我们必须时时刻刻清醒地看待自己,做到不卑不亢才能坚强地活下去. 请肆无忌惮地点赞吧,微信搜索[沉默王二]关注这个在九朝古都洛阳苟且偷生的程序员.本文 GitHub github.com/i ...