Reputation: 69
I get this error:
Traceback (most recent call last): File "C:/Python27/main.py", line 21, in matrix[1][1].append(2) IndexError: list index out of range
This is my code
file = open("C:\\Python27\\test.txt", "r")
s1 = file.read();
s2 = file.read();
matrix = [[0 for x in range(len(s1))] for x in range(len(s2))]
matrix[1][1].append(2)
print matrix[1][1]
len(s1)
and len(s2)
is larger than 5
I try using matrix[1][1] = 2
instead of matrix[1][1].append(2)
but it won't work.
So what's my mistakes?
Upvotes: 0
Views: 640
Reputation: 96
I'd print len(s1) and len(s2). They are probably not what you expect. And if you want to set row 1 col 1 of matrix you would do:
matrix[1][1] = 2
because otherwise you are trying to append number 2 to element at (1,1) which is not a list.
Upvotes: 3