Python:TypeError:list indices必须是整数,而不是str

Cha*_*kul 10 python matrix python-2.x typeerror

我要在Python上做Matrix Addition.(没有完成).但它显示错误.

m, n = (int(i) for i in raw_input().split())
a = [[0 for i in range(m)] for j in range(n)]
b = [[0 for i in range(m)] for j in range(n)]
c = []
total = []

for i in range(m):
    x = raw_input()
    for j in range(n):
        value = [int(i) for i in x.split()]
    c[i][j] = a[i][j]
    #c.append(value)
print a
for i in c:
    print i
Run Code Online (Sandbox Code Playgroud)

我想输入

3 3 < - 矩阵维m*n

1 2 3>

3 2 1>矩阵A.

1 3 2>

1 1 1>

1 1 1>矩阵B.

1 1 1>

并显示为

2 3 4>

4 3 2>矩阵A + B.

2 4 3>

cda*_*rke 7

您正在使用i外部for循环,它是一个int.然后在循环中你有:

value = [int(i) for i in x.split()]
Run Code Online (Sandbox Code Playgroud)

这使得i一个字符串(这是split返回).也许你认为里面有某种范围[ ]?没有.你有一个名字冲突,改变其中一个.