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

前端 未结 2 827
轻奢々
轻奢々 2020-12-20 19:36

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.

2条回答
  •  执念已碎
    2020-12-20 20:14

    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.

提交回复
热议问题