[[' 1',' 108',' 109',' 100',' 101'] ,[],[],[],[]]
[[' 102',' 103',' 104',' 105',' 106'] ,[' 102',' 103',' 104',' 105',' 106'],[] ,[],[]]
[[' 107',' 108',' 109',' 100',' 101'] ,[' 107',' 108',' 109',' 100',' 101'],[& #39; 107',' 108',' 109',' 100',' 101'],[],[] ]
但我希望我的输出如下:
[[' 101',' 102',' 103',' 104',' 105'] ,[],[],[],[]]
[[' 101',' 102',' 103',' 104',' 105'] ,[' 106',' 107',' 108',' 109',' 101'],[] ,[],[]]
[[' 101',' 102',' 103',' 104',' 105'] ,[' 106',' 107',' 108',' 109',' 101'],[& #39; 102',' 103',' 104',' 105',' 106'],[],[] ]
import serial
row=5
col=3
multiList = ([[], [], [], [], []])
ser = serial.Serial(COM4, baudrate=115200, timeout=1)
numPoints = 5
dataList = [0] * numPoints
def getValues():
ser.write(b'g')
fpgadata = ser.readline().decode('ascii').split('\n')
return fpgadata[0]
def update():
for j in range(3):
for i in range(0, numPoints):
dataList[i] = getValues()
#print(dataList)
multiList[j] = dataList
print(multiList)
update()
答案 0 :(得分:1)
问题是,在multiList[j] = dataList
步骤中,您不会复制元素,只会复制dataList
副本。所以实际上multiList
包含对同一列表的多个引用。要将元素复制到新列表中,请编写multiList[j] = dataList[:]
,如下所示:
def update():
for j in range(3):
for i in range(0, numPoints):
dataList[i] = getValues()
multiList[j] = dataList[:]
print(multiList)
这将为multiList[j]
新列表分配所需的效果,
已复制dataList
元素。