矩阵的标量乘法(int 不可下标)

Scalar multiplication of matrix (int is not subscriptable)

问题: 给定一个 M*N 矩阵和一个变量 K,打印标量乘法后的结果矩阵。 我写的代码:

n,m,k=map(int,input().split())
mat1=[]
for i in range(n):
    mat1.append(list(map(int,input().split())))

for i in range(n):
    for j in range(m):
        mat1=mat1[i][j] * k
for i in range(n):
    for j in range(m):
        print(mat1[i][j],end=" ")
    print()

错误:

Traceback (most recent call last):
  File "C:/Users/91934/PycharmProjects/pythonProject/main.py", line 8, in <module>
    mat1=mat1[i][j] * k
TypeError: 'int' object is not subscriptable

您正在用这一行的数字值替换列表变量 mat1,导致上述错误:

mat1=mat1[i][j] * k

相反,您应该指定要按索引分配的嵌套列表项:

mat1[i][k] = mat1[i][j] * k

或:

mat1[i][j] *= k