Python矩阵索引(Python matrix indexing)
我有以下代码
l = len(time) #time is a 300 element list ll = len(sample) #sample has 3 sublists each with 300 elements w, h = ll, l Matrix = [[0 for x in range(w)] for y in range(h)] for n in range(0,l): for m in range(0,ll): x=sample[m] Matrix[m][n]= x当我运行代码来填充矩阵时,我得到一条错误消息,说“列表索引超出范围”我输入了一个print语句来查看错误发生的位置,当m = 0且n = 3时,矩阵超出索引。
根据我对代码第四行的理解,我初始化了一个3X300矩阵,那为什么它会在0X3时超出索引?
I have the following code
l = len(time) #time is a 300 element list ll = len(sample) #sample has 3 sublists each with 300 elements w, h = ll, l Matrix = [[0 for x in range(w)] for y in range(h)] for n in range(0,l): for m in range(0,ll): x=sample[m] Matrix[m][n]= xWhen I run the code to fill the matrix I get an error message saying "list index out of range" I put in a print statement to see where the error happens and when m=0 and n=3 the matrix goes out of index.
from what I understand on the fourth line of the code I initialize a 3X300 matrix so why does it go out of index at 0X3 ?
最满意答案
您需要将Matrix[m][n]= x更改为Matrix[n][m]= x
You need to change Matrix[m][n]= x to Matrix[n][m]= x
更多推荐
发布评论