Python矩阵索引(Python matrix indexing)

系统教程 行业动态 更新时间:2024-06-14 17:02:18
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]= x

When 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

更多推荐

本文发布于:2023-04-21 18:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/649510de3a3016e2a905cbd3af84186e.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:矩阵   索引   Python   matrix   indexing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!