EDITED ALL
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)
how to declare array of struct in Python?
FDResult array and blank data inside,Length of bla should equal 0
如何在Python中声明struct数组? FDResult数组和空白数据,bla的长度应该等于0
ANSWER
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)
2 个解决方案
#1
0
You can create namedtuple array like this.
您可以像这样创建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)
#2
0
If length of bla
should be zero, just create an empty list:
如果bla的长度应为零,则只需创建一个空列表:
bla = []
A Python list can contain objects of any type. If bla
is only supposed to hold FDResult
instances, it is your responsibility as programmer not to put anything else there.
Python列表可以包含任何类型的对象。如果bla只应该保存FDResult实例,那么程序员不应该在那里放任何其他内容。
#1
0
You can create namedtuple array like this.
您可以像这样创建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)
#2
0
If length of bla
should be zero, just create an empty list:
如果bla的长度应为零,则只需创建一个空列表:
bla = []
A Python list can contain objects of any type. If bla
is only supposed to hold FDResult
instances, it is your responsibility as programmer not to put anything else there.
Python列表可以包含任何类型的对象。如果bla只应该保存FDResult实例,那么程序员不应该在那里放任何其他内容。