TypeError: list indices must be integers, not a list. How to fix?

Here is the code with a TypeError in it. "List indices should be integers, not a list," although they are integers. I would be grateful if you could help me figure out what happened. I need to get the 7x5 matrix from the 7x5 tab of the source code with different values. Error on the last line.

lines = []

with open("text.txt") as f:
    for line in f:
        line = [int(x) for x in line if (x != ' ') and (x != '\n')]
        lines.append(line)
    f.close()

What I have after reading the file is a list of lists with numbers called "strings". These are integers. Not strings. How:

>> [[1, 2, 3...], [4, 5, 6...], [7, 8, 9...],[...]]


i = 1
j = 1
T = []
T.append(lines[0][0]) 

I did this to avoid IndexError(list out of range) on the last line ( i-1and stuff). Although, I do not think this is really a python-way. I would appreciate help on this.

for i in lines:
    for j in lines:
        T[i][j] = lines[i][j] + max(T[i][j-1], T[i-1][j])

. , , i, j int.

+4
2

i j ; lines. Python for .

:

for i, line in enumerate(lines):
    for j, value in enumerate(line):
        T[i][j] = value + max(T[i][j - 1 % len(T[i])] + T[i - 1 % len(T)][j])

% len() "" T T[i], i / j 0. enumerate() .

, T.

+5
for i in lines:
    for j in lines:

i j lines, . , i j , .

( , ),

for i in range(len(lines)):
    for j in range(len(lines[i])):

, Python , .

, lines . , .

+3

Source: https://habr.com/ru/post/1531980/


All Articles