There\'s the code with the TypeError in it. \"list indices must be integers, not list\", though they are integers. I\'d appreciate you helping me figure out what\'s wrong.
i
and j
are not indices; they are values from the lines
list. Python for
loops are for-each constructs.
Use:
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])
where the % len()
calculations 'wrap around' to the last entry in T
or T[i]
when i
and / or j
are 0
. The enumerate() function adds indices to the loop.
This does assume you already pre-built a nested list of lists structure in T
.