Python: TypeError: list indices must be integers, not str [duplicate]
Asked Answered
H

5

10

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 >

Hydromancy answered 22/9, 2014 at 11:49 Comment(2)
Try to change value = [int(i) for i in x.split()] to value = [int(k) for k in x.split()] ? You are using i in the for loop + in the list comprehension.Barina
Since 0 is an immutable object, you can simplify to a = [ [0]*m for j in range(n) ].Auger
P
7

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.

Parcenary answered 22/9, 2014 at 12:0 Comment(2)
+1 Note that Python 3 does create a new scope for comprehensions.Auger
@chepner: well spotted.Parcenary
L
1

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
Lotuseater answered 22/9, 2014 at 11:59 Comment(0)
N
1

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!

Nephrosis answered 22/9, 2014 at 12:6 Comment(0)
H
0
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

Hydromancy answered 30/9, 2014 at 15:48 Comment(0)
P
0

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
Philina answered 8/4, 2015 at 0:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.