gpt4 book ai didi

python - 编辑 : how to declare array of struct in Python

转载 作者:太空宇宙 更新时间:2023-11-04 04:51:23 25 4
gpt4 key购买 nike

全部编辑

import struct
from collections import namedtuple

FDResult = namedtuple('FDResult', ['DeviceID', 'PageNum'])
#bla = [FDResult(DeviceID='NR0951113', PageNum=[1,2,3,4]),
#FDResult(DeviceID='NR0951114', PageNum=[17,28,63,64]),
#FDResult(DeviceID='NR0951115', PageNum=[2,3,4,5])]

bla = [FDResult(DeviceID='NR0951115', PageNum=[1])] #how to declare bla as FDResult array and blank data inside,Length of bla should equal 0
bla.append(FDResult(DeviceID='NR0951112', PageNum=[2]))
print(len(bla))

bla[0].PageNum.append(16)

如何在Python中声明结构数组?
FD结果数组和里面的空白数据,bla的长度应该等于0

回答

FDResult = namedtuple('FDResult', ['DeviceID', 'PageNum'])
#bla = [FDResult(DeviceID='NR0951113', PageNum=[1,2,3,4]),
#FDResult(DeviceID='NR0951114', PageNum=[17,28,63,64]),
#FDResult(DeviceID='NR0951115', PageNum=[2,3,4,5])]
NodeList = []

Node = FDResult(DeviceID='NR0951113', PageNum=[1,2,3,4])
NodeList.append(Node)
print(len(NodeList))
NodeList[0].PageNum.append(16)

最佳答案

您可以像这样创建 namedtuple 数组。

from collections import namedtuple

MyStruct = namedtuple('MyStruct', 'Mark nPackLen nFlag nGisIp nPort sData sEnd')

NodeList = []
Node = MyStruct(None, '', '', '', '', -1, 0)
for id in range(4):
NodeList.append(Node)

关于python - 编辑 : how to declare array of struct in Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14725253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com