Python:类型错误:列表索引必须是整数,而不是 str

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25973413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:53:46  来源:igfitidea点击:

Python: TypeError: list indices must be integers, not str

pythonmatrixtypeerrorpython-2.x

提问by Chalanthorn Chanmathikornkul

I'm going to do Matrix Addition on Python.(Not finish). But it shows an error.

我将在 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


I want to input

我要输入

3 3 <-- matrix dimensional m*n

3 3 <-- 矩阵维数 m*n

1 2 3 >

1 2 3 >

3 2 1 > matrix A

3 2 1 > 矩阵 A

1 3 2 >

1 3 2 >

1 1 1 >

1 1 1 >

1 1 1 > matrix B

1 1 1 > 矩阵 B

1 1 1 >

1 1 1 >

and shows as

并显示为

2 3 4 >

2 3 4 >

4 3 2 > matrix A + B

4 3 2 > 矩阵 A + B

2 4 3 >

2 4 3 >

采纳答案by cdarke

You are using iin your outer forloop, and it is an int. Then in the loop you have:

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

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

which makes ia string (which is what splitreturns). Maybe you think there is some sort of scoping inside [ ]? There isn't. You have a name collision, change one of them.

它生成i一个字符串(这是split返回的内容)。也许你认为里面有某种范围界定[ ]?没有。您遇到了名称冲突,请更改其中之一。

回答by Nilesh

You are using same variable in inner for loop.

您在内部 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

回答by cezar

Beyond the first two answers you'll have a problem with this statement:

除了前两个答案之外,您还会遇到以下语句的问题:

c[i][j] = a[i][j]

When the loop starts iwill be 0 and that's so far OK, but cis an empty list and has no iterable at the first position so c[0][0]will return an error. Get rid of it and uncomment the following line:

循环开始时i将为 0,到目前为止还可以,但它c是一个空列表,并且在第一个位置没有可迭代对象,因此c[0][0]将返回错误。摆脱它并取消注释以下行:

#c.append(value)

EDIT:

编辑:

Your code won't return what you want. You'd better make something like this to create a matrix with the given sides:

您的代码不会返回您想要的内容。你最好做这样的事情来创建一个给定边的矩阵:

for i in range(m):
    d = []
    for j in range(n):
        x = raw_input()
        d.append(int(x))
     c.append(d)

If you have 3 for both mand n, then you will create matrix with sides 3 x 3 saved in the variable c. In this way you don't have to split the user input. The user can give a number at a time. And you could even change the following line:

如果您有 3m和3 n,那么您将创建边 3 x 3 保存在变量中的矩阵c。通过这种方式,您不必拆分用户输入。用户可以一次给出一个号码。您甚至可以更改以下行:

x = raw_input()

to:

到:

x = raw_input("{0}. row, {1}. column: ".format(i+1, j+1))

Try it out!

试试看!

回答by Chalanthorn Chanmathikornkul

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)

OK! I just figured it out! Thank you

好的!我刚刚想通了!谢谢

回答by r_2

you can also hit this error if you declare an int and treat it like a dict

如果您声明一个 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