本文介绍了Python如何在存储在列表中的namedtuple中编辑数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
import struct
from collections import namedtuple
StructPageNum = namedtuple('FDResult', ['DeviceID', 'PageNum','PicSize','PicData'])
PageNumList = []
Node = StructPageNum(DeviceID='NR09', PageNum=[],PicSize=100,PicData='')
PageNumList.append(Node)
PageNumList[0].PicData = 'hello' //how to do at here?
问题
如何编辑值
QUESTION
how to edit the value of PicData?
推荐答案
在我看来,您可以使用方法,可以很容易地做到这一点:
It looks to me like you can use the _replace
method of a namedtuple to do this pretty easily:
PageNumList[0] = PageNumList[0]._replace(PicData='hello')
这会在您的 PageNumList
中放入一个新的namedtuple 看起来很像旧的namedtuple,只不过我们更改了 PicData
属性。您不能编辑已经存在的namedtuple,因为 namedtuple
是不可变的(就像它们的未命名对应项一样: tuples
)
This puts a new namedtuple in your PageNumList
which looks pretty much like the old namedtuple except that we've changed the PicData
"attribute". You can't edit the namedtuple that you have already since namedtuple
s are immutable (just like their unnamed counterparts: tuples
)
这篇关于Python如何在存储在列表中的namedtuple中编辑数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!