我有一个包含5个元素的单个维度列表'dataList'和一个包含20行和5列的多维列表'multiList'。dataList中的数据来自串行通信。 我想做的是逐行将dataList添加到multiList中,如果20行的限制超过新行必须替换旧行,即第21行应该替换第1行,第22行应该替换第2行。 我附上了代码。
multiList = [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],
[],[],[],[]]
row=20
col=4
def update():
if getValues() == b'104'.decode('ascii'):
for i in range(0, numPoints):
dataList[i] = getValues()
for x in range (row):
for y in range (5):
multiList[x].append(dataList[y])
print(multiList)
答案 0 :(得分:0)
如果我正确理解您的问题... dataList的列固定为5。 更新函数被调用一次,一个dataList进入。
该python代码应该是......
multiList = [[0]*5]*20 # initialization (20, 5)
row=20
col=5
current_index = 0
dataList=[0]*5
def update():
global current_index
if getValues() == b'104'.decode('ascii'):
for i in range(0, numPoints): # it seems that numPoints should be 'col'
dataList[i] = getValues()
multiList[current_index] = list(dataList)
current_index = (current_index+1)%row
print(multiList)