Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:TypeError:list索引必须是整数,而不是str_Python_Matrix_Typeerror_Python 2.x - Fatal编程技术网

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

Python:TypeError:list索引必须是整数,而不是str,python,matrix,typeerror,python-2.x,Python,Matrix,Typeerror,Python 2.x,我将在Python上进行矩阵加法(未完成)。但它显示了一个错误 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):

我将在Python上进行矩阵加法(未完成)。但它显示了一个错误

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

我想输入

3 3

3 2 1>矩阵A

1 3 2>

1>

1>矩阵B

1>

并显示为

2 3 4>

4 3 2>矩阵A+B


2 4 3>

在内部for循环中使用相同的变量

for i in range(m):
    x = raw_input()
    for j in range(n):
        # variable i is refering to outer loop
        value = [int(p) for p in x.split()]
    c[i][j] = a[i][j]
    #c.append(value)
print a
for i in c:
    print i

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

value = [int(i) for i in x.split()]

这使得
i
成为一个字符串(这就是
split
返回的内容)。也许您认为
[]
中存在某种范围界定?没有。如果出现名称冲突,请更改其中一个。

除了前两个答案之外,您将对该语句产生问题:

c[i][j] = a[i][j]
当循环开始时,
i
将为0,到目前为止还可以,但是
c
是一个空列表,在第一个位置没有iterable,因此
c[0][0]
将返回一个错误。 删除它并取消对以下行的注释:

#c.append(value)
x = raw_input()
编辑:

你的代码不会返回你想要的。您最好制作如下内容,以创建具有给定边的矩阵:

for i in range(m):
    d = []
    for j in range(n):
        x = raw_input()
        d.append(int(x))
     c.append(d)
如果
m
n
都有3,那么您将创建一个矩阵,其边3 x 3保存在变量
c
中。 这样,您就不必拆分用户输入。用户可以一次给出一个数字。您甚至可以更改以下行:

#c.append(value)
x = raw_input()
致:

试试看

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

for i in range(m):
    x = raw_input()
    for j in range(n):
        value = [int(i) for i in x.split()]
    a.append(value)
#print a


for i in range(m):
    x = raw_input()
    for j in range(n):
        value = [int(i) for i in x.split()]
    b.append(value)
#print b


for i in range(m):
    for j in range(n):
        total[i][j] = a[i][j] + b[i][j]


for i in total:
    print ' '.join(map(str, i))
time.sleep(2)

好的!我刚想出来!谢谢

如果声明一个int并将其视为dict,您也可以遇到此错误

>>> a = []
>>> a['foo'] = 'bar'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not str
>a=[]
>>>a['foo']='bar'
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:列表索引必须是整数,而不是str

尝试将
value=[int(i)表示x.split()中的i]
更改为
value=[int(k)表示x.split()中的k]
?您正在列表理解中的for循环+中使用
i
。由于
0
是一个不可变的对象,您可以简化为
a=[[0]*m代表范围(n)]
+1请注意,Python 3确实为理解创建了一个新的范围。@chepner:很好。