collections 数据结构模块namedtuple
namedtuple类
导入模块
from collections import namedtuple
使用方法及说明
#pycharm 里按住 ctrl键点击 collections可查看源码
#collections数据结构
#__all__ = ['deque', 'defaultdict', 'namedtuple', 'UserDict', 'UserList','UserString', 'Counter', 'OrderedDict', 'ChainMap']
使用说明:
#猜包功能
name = ("lijie",'ll')
user = ("aa",23,189,'boddy') username,age,height,edu = user
print (username,age,height,edu)
打印结果:
aa 23 189 boddy #另一种方法
username,*other = user
print (username,other)
打印结果:
aa [23, 189, 'boddy']
namedtuple
功能说明:
相当于创建一个类,并调用类的属性的值
#传统类的属性值调用
class User1(object):
def __init__(self,name,age,height):
self.name = name
self.age = age
self.height = height
常规操作
#使用namedtuple,namedtuple是tuple的子类,相比于class,节省空间,代码变少,使用方法如下:
User = namedtuple("user",['name','age','height']) #相当于类的初始化,初始化变量信息
user = User(name='test',age=23,height=111) #给各参数赋值
print (user.name,user.age,user.height) #直接打印类的属性的值 #增加一列(*args方式,元组)
User = namedtuple("user",['name','age','height',"edu"]) #假如edu为新增加的列
user = ('test',23,111) #元组
user_end = User(*user,"master") ##将数组传到namedtuple里,master为添加的edu的信息
print (user_end.name,user_end.age,user_end.height,user_end.edu) #以**kwargs的方式增加列或传入数据(字典)
User = namedtuple("user",['name','age','height',"edu"])
user = { #字典
"name":'test',
"age":23,
"height":111
}
user_end = User(**user,edu="master") ##将字典数据传到namedtuple里print (user_end.name,user_end.age,user_end.height,user_end.edu) #_make方法
##如果用_make方法,可以把“*”省略,但是要求tuple或者dict里的元素数量必须与nametuple里指定的属性数量相同,例:
user1 = {
"name":'test',
"age":23,
"height":111,
"edu":"aa"
}
user_end = User._make(user1)
print (user_end.name,....) #当然,namedtuple也支持猜包
name,age,*other = user_end
print (name,age,other)
额外补充
tuple 可作为字典的key,而list不可以,示例:
name_tuple = ("test",22,185,"baskerball")
name_list = ["test1",22,188,"baseball"]
dd = {}
dd[name_tuple] = 'boddy'
print (dd)
dd[name_list] = 'body'
print (dd)
打印结果:
{('test', 22, 185, 'baskerball'): 'boddy'}
Traceback (most recent call last):
File "D:/python-script/collections_module/chapter1/collection_module.py", line 29, in <module>
dd[name_list] = 'body'
TypeError: unhashable type: 'list'
collections 数据结构模块namedtuple的更多相关文章
- collections集合模块 [namedtuple,deque,*]
collections是Python内建的一个集合模块,提供了许多有用的集合类. namedtuple namedtuple是一个函数, 它用来创建一个自定义的tuple对象,并且规定了 tuple元 ...
- collections模块-namedtuple
namedtuple -> 命名元组 这里的命名指的是对元组中元素的命名. 通过一个例子来看 import collections Person = collections.namedtuple ...
- python基础 ---time,datetime,collections)--时间模块&collections 模块
python中的time和datetime模块是时间方面的模块 time模块中时间表现的格式主要有三种: 1.timestamp:时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算 ...
- 25、typing导入Python的数据类型模块、collections集合模块
一.typing模块 1.typing模块的作用 类型检查,防止运行时出现参数和返回值类型不符合. 作为开发文档附加说明,方便使用者调用时传入和返回参数类型. 该模块加入后并不会影响程序的运行,不会报 ...
- python基础语法12 内置模块 json,pickle,collections,openpyxl模块
json模块 json模块: 是一个序列化模块. json: 是一个 “第三方” 的特殊数据格式. 可以将python数据类型 ----> json数据格式 ----> 字符串 ----& ...
- The Collections Module内建collections集合模块
https://www.bilibili.com/video/av17396749/?p=12 Python函数式编程中的迭代器,生成器详解 课程内容 1.iterators are objects ...
- collections库的namedtuple+pytest的使用
from collections import namedtupleTask=namedtuple('Task',['summary','owner','done','id'])Task.__new_ ...
- fis中的数据结构模块Config
/* * config * caoke */ 'use strict'; Object.extend=function(props){ //继承父类 var prototype=Object.crea ...
- Python高级数据结构-Collections模块
在Python数据类型方法精心整理,不必死记硬背,看看源码一切都有了之中,认识了python基本的数据类型和数据结构,现在认识一个高级的:Collections 这个模块对上面的数据结构做了封装,增加 ...
随机推荐
- docker资料---仓库搭建
以下步骤均为本人亲自踩坑,历经数次失败,最终搭建成功 一.环境信息: 操作系统:CentOS 7 Docker版本:1.12.5 (更高版本应该类似) registry:2.4.1 registry- ...
- 第二十二天 logging hashlib re 模块
今日内容 logging功能完善的日志模块 re正则表达式模块主要处理字符串匹配 查找 搜索给你一个字符串 要从中找到你需要的东西 爬虫大量使用 hashlib hash算法相关的库 算法怎么算不需要 ...
- FPGA时序分析相关
什么叫时序? 时间与动作的相互关系,什么时间干什么活. 同步时序:单一时钟源,所有寄存器在单一时钟源下同步工作. 异步时序:多个时钟源,除使用带时钟的触发器之外,还可以使用不带时钟的触发器与延时元件作 ...
- 【XSY2773】基因 后缀平衡树 树套树
题目描述 有一棵树,每条边上面都有一个字母.每个点还有一个特征值\(a_i\). 定义一个节点\(i\)对应的字符串为从这个点到根的路径上所有边按顺序拼接而成的字符串\(s_i\). 有\(m\)次操 ...
- 【XSY2707】snow 线段树 并查集
题目描述 有\(n\)个人和一条长度为\(t\)的线段,每个人还有一个工作范围(是一个区间).最开始整条线段都是白的.定义每个人的工作长度是这个人的工作范围中白色部分的长度(会随着线段改变而改变).每 ...
- MT【269】含参函数绝对值最大
设函数$f(x)=ax^2+(2b+1)x-a-2$($a,b\in\mathcal R$,$a\neq 0$). (1) 若$a=-2$,求函数$y=|f(x)|$在$[0,1]$上的最大值$M(b ...
- 【HDU - 5845】Best Division(xor-trie、01字典树、dp)
BUPT2017 wintertraining(15) #7E 题意 把数组A划分为k个区间,每个区间不超过L长度,每一个区间异或和之和为S.现在求:S不超过X,区间个数的最大值. 且A是这样给你的: ...
- ⌈洛谷1312⌋⌈NOIP提高组2011⌋Mayan游戏【搜索】
感想 真的,感觉这道题目好坑爹,我这个蒟蒻调了好几个世纪才调出来. 重构代码千万遍,依旧只有-1输出. 正解 非常明显的一道搜索题目. 每一次记录上一级的状态,这样实现比较不容易出错. 然后考虑剪枝: ...
- 20165223 Linux安装及命令入门
预备作业3:Linux安装及命令入门 一.VirtualBox和Ubuntu的安装 通过学习实践基于VirtualBox虚拟机安装Ubuntu图文教程,我开始学习虚拟机的安装,根据教程一步步试着安装. ...
- postgreSQL学习(一):在Linux下安装postgreSQL
1. 安装源: $ sudo yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgd ...