简体   繁体   English

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

[英]Python: TypeError: list indices must be integers, not str

I'm going to do Matrix Addition on Python.(Not finish). 我要在Python上做Matrix Addition。(没有完成)。 But it shows an error. 但它显示错误。

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>

You are using i in your outer for loop, and it is an int. 你在你的外部for循环中使用i ,它是一个int。 Then in the loop you have: 然后在循环中你有:

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

which makes i a string (which is what split returns). 这使i成为一个字符串(这是split返回)。 Maybe you think there is some sort of scoping inside [ ] ? 也许你认为[ ]里面有某种范围? There isn't. 没有。 You have a name collision, change one of them. 你有一个名字冲突,改变其中一个。

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

Beyond the first two answers you'll have a problem with this statement: 除了前两个答案之外,您将对此声明产生问题:

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

When the loop starts i will be 0 and that's so far OK, but c is an empty list and has no iterable at the first position so c[0][0] will return an error. 当循环开始时i将为0并且到目前为止确定,但是c是空列表并且在第一个位置没有可迭代,因此c[0][0]将返回错误。 Get rid of it and uncomment the following line: 摆脱它并取消注释以下行:

#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 m and n , then you will create matrix with sides 3 x 3 saved in the variable c . 如果mn都有3,那么你将创建保存在变量c中的边3 x 3的矩阵。 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: 你甚至可以改变以下行:

x = raw_input()

to: 至:

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

Try it out! 试试看!

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 谢谢

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 &#39;TypeError:列表索引必须是整数,而不是str&#39;Python 3 - 'TypeError: list indices must be integers, not str' Python 3 类型错误:列表索引必须是整数,而不是 str - python - TypeError: list indices must be integers, not str - python TypeError:list indices必须是整数,而不是str Python - TypeError: list indices must be integers, not str Python TypeError:列表索引必须是整数,而不是Python中的str - TypeError: list indices must be integers, not str in Python Python 类型错误:列表索引必须是整数,而不是 str - Python TypeError: list indices must be integers, not str Python-&gt; TypeError:列表索引必须是整数,而不是str - Python -> TypeError: list indices must be integers, not str Python列表循环错误:TypeError:列表索引必须是整数,而不是str - Python List Loop Error: TypeError: list indices must be integers, not str Python3-TypeError:列表索引必须是整数或切片,而不是str-List - Python3 - TypeError: list indices must be integers or slices, not str - List 使用Python解析JSON:TypeError:list indices必须是整数,而不是str - Parsing JSON with Python: TypeError: list indices must be integers, not str Python chatbot-TypeError:列表索引必须是整数,而不是str - Python chatbot - TypeError: list indices must be integers, not str
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM