ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC=1,5] >[success] # 命名元组 -namedtuple 命名元组,类似字典,相比普通元组,只增加了极小的开销提供了这些便利,的一种可能用法是作为字典的替代,后者需要更多的空间来储存,因此如果涉及字典的大型数据结构可以考虑命名元组,会更高效 >[info] ## 使用 ~~~ def namedtuple(typename, field_names, verbose=False, rename=False) 1.第一个参数,typename 是元组的名字 2.field_names 字段的名字可以理解成字典的k 3.renme 是否可以重名,或者使用python关键字 4.verbose 是否在控制台输出,具体的定义展示 ~~~ * 注 rename,会吧关键字和重复的属性用_的形式重新命名 ~~~ from collections import namedtuple nametuple = namedtuple("nametuple","addr joined class age age",rename=True) nt = nametuple("addr","joined","class","age","age2") print(nt) 打印结果: nametuple(addr='addr', joined='joined', _2='class', age='age', _4='age2') ~~~ * 注 使用verbose ~~~ from collections import namedtuple nametuple = namedtuple("nametuple","addr joined age",verbose=True) nt = nametuple("addr","joined","class","age","age2") print(nametuple) 打印结果: from builtins import property as _property, tuple as _tuple from operator import itemgetter as _itemgetter from collections import OrderedDict class nametuple(tuple): 'nametuple(addr, joined, age)' ......... ~~~ >[danger] ##### 基本使用 ~~~ from collections import namedtuple # 另外两种使用情况见备注 nametuple = namedtuple("nametuple",["addr", "joined"]) nt = nametuple('###addr',"####joined") print(nametuple) print(nt) print(nt.addr) print(nt.joined) 打印结果: <class '__main__.nametuple'> nametuple(addr='###addr', joined='####joined') ###addr ####joined ~~~ * 注: ~~~ nametuple = namedtuple("nametuple","addr, joined") nametuple = namedtuple("nametuple","addr joined") ~~~ >[danger] ##### 支持普通元组的操作 ~~~ from collections import namedtuple nametuple = namedtuple("nametuple","addr joined") nt = nametuple('###addr',"####joined") addr,joined = nt print(addr,joined) print(len(nt)) 打印结果: ###addr ####joined 2 ~~~ >[danger] 技巧使用-- ([ ],[ ]) * 当处理一些元组嵌套列表,列表中的内容可能会出现变化的情况,例如从数据库中取出一组数据 * 推荐下面的方式二 ~~~ # 下面的代码假如新增了数据,就会出现因为下角标定位的问题 def compute_cost(records): total = 0.0 for rec in records: total += rec[1]*rec[2] return total # 我叫方式二 Stock = namedtuple("Stock",['name',"age","price"]) def compute_cost(records): total = 0.0 for rec in records: s = Stock(*rec) total += s.age * s.price return total print(compute_cost(a)) ~~~ >[danger] ##### _asdict() 转换成有序的字典 ~~~ from collections import namedtuple nametuple = namedtuple("nametuple","addr joined class age age",rename=True) nt = nametuple("addr","joined","class","age","age") nt_dic = nt._asdict() print(nt_dic) ~~~ >[danger] ##### 更改值 * 直接改会报错 ~~~ from collections import namedtuple nametuple = namedtuple("nametuple","addr joined age") nt = nametuple("addr","joined","age") # nt.joined =10 ~~~ * 未定义更改 ~~~ from collections import namedtuple nametuple = namedtuple("nametuple","addr joined age") nt = nametuple("addr","joined","age") nt._replace(age = "10") print(nt) 打印结果: nametuple(addr='addr', joined='joined', age='age') ~~~ * 定义更改 ~~~ from collections import namedtuple nametuple = namedtuple("nametuple","addr joined age") nt = nametuple("addr","joined","age") # 重新定义接受变量 nt = nt._replace(age = "10") print(nt) 打印结果: nametuple(addr='addr', joined='joined', age='10') ~~~ >[danger] ##### 字典转换成命名元组 ~~~ def dict_to_stock(s): return namedtuple.__replace(**s) ~~~