简体   繁体   中英

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

I'm going to do Matrix Addition on Python.(Not finish). 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

1 2 3 >

3 2 1 > matrix A

1 3 2 >

1 1 1 >

1 1 1 > matrix B

1 1 1 >

and shows as

2 3 4 >

4 3 2 > matrix A + B

2 4 3 >

You are using i in your outer for loop, and it is an 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). 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 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. 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 . 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

>>> a = []
>>> a['foo'] = 'bar'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not str

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM