首页 > 解决方案 > 为什么在遍历列表矩阵时出现列表索引超出范围错误?

问题描述

我正在尝试遍历矩阵列表,即(向右移动 3 个值,向下移动 1 个值)并看到上述错误。预期值正在打印到控制台,这表明该值存在并且没有超出范围。

grid = []

with open(filename) as f:
    for line in f.readlines():
        line.strip()
        grid.append(line)
print(grid)

col = 2
row = 0
count = 0

for i in enumerate(grid):
    row = row + 1
    print(grid[row][col])
    if grid[row][col] == "#":
        print("found")
        print(grid[row][col])
        count = count+1
    col = col + 3

如果我不增加 row 和 col 值,这个问题也会持续存在。我的输入数据在这里:

.........###......#...#.......#
.#.#...........#..#..#.........
#.......#.................#....
.........#.#.........#......###
.....#......##...##............
...##...#......#.....#.....##..
#.#..#....#...#....#......#....
........##.....#.....#.#.......
......#.....#......#...##.#....
...####...#.......##.....#.#...
.........#....#......##........
..##...........###.#...........
.....#............#............
#.#..#..##........#.....#..#...
.....................#....##..#
...........##.....###...#.#.#..
..#......#...........#.........
.##.##...#...#......##.#.......
......#..#......#.#.#..#.#.....
........#.#..#..........#...#.#
...........##...........#....#.
...........#...##.#............
.......#...........#...........
.......#......#..#...#....#..#.
..#.....#.#....#.#......#...#.#
.#..........###..#....#........
........##..#..#...#..#....#..#
#..........#...#..#.#........#.
..#.#........##.##....##.#.....
#.##....#...#.......#.#..#....#
......##...#.#.#.#.....#....#..
..........#..............#.....
....................###.#......
#.....#...#...#.#.......#....#.
.......#..#...................#
........##.##........#......#..
...#...##.#...#...........#....
..#.........#...#....##......#.
......#..............#..#..#.#.
.....##...#...#...##....#......
#.#....#...#......##.....#...##
.#...#.#..................#....
#.##.....#......#..........#...
..#..#.......#.................
..#.....#.........#........#...
.......#...##.##.#..#..##.#..#.
#.............#.........#.#....
..#..##..........#..#..##.#.#..
.#......#.......#...#.....##.#.
.....#......#...#...........###
..........#.........#.....#....
.....#..........#.......##...#.
......#..#..#..............#...
.#.####..#...##...#.#..........
..#....#.......#........#.....#
....#.##.....#..#.....#.#.#..#.
.......#..#..##.......#........
.#.....#...........#.....#.....
........#..........##..##.#....
.#.....#........#.....#..#.....
..#..........#...#......##..#..
.#............#.........#....#.
........#..###.......#.....###.
##.#...#.#..#..#..#.#.##...#...
.#....#...#..#......##.........
.............##.....##.........
.....##.#..###.#....#...#...#.#
#........#...#......#...#.##...
#....#......#.....###.##.#.....
.....#..#.#.##....#..##.....##.
....#...#...#..........##......
..........#......#...#.....##..
.....##....##.#.............#.#
#.........#.##.............#..#
.....#.........##.#...#.#.#....
..........#..#......#..#.....#.
....#....#....#....#.......###.
....#...#..##....#..##..#...##.
.###......#...........###......
#..................####.#....#.
#....#.#.....#.#....#..#.......
...#......#....##......#..#..#.
#.#...#.##.....#.#.......##..#.
.........##.................#..
#..##.#....#.#.............#...
....................####.#.#..#
.......#..#...#..#..#.....#...#
.....#.#.#........#....#...##..
......#..#....#......#..##.....
............#......##.#....#..#
...#..........#..#...........#.
..........#.............###....
....##.#.#......#.#..#....##..#
..#..........#........#......#.
..#...........####......##..#.#
...##......##...#..#.##........
.....#...#.....##.....###..##..
.#.##.....#....##....#.........
#....##..#.....#.#......#.#....
..#.......#...#....#...#.#.....
...........#.........#.........
..#..#....##..#....#....#.....#
.......#..#....##....#.........
#.........#...........##....##.
#........#.#...............##..
#...##.#...............#.......
#....#..#......#..#.###...##..#
..#.........#.#......#.....#..#
......#...........##........##.
.#.........#................#..
#...#...............#...#....#.
.#.#......##.........#.#.......
..........#....................
.#.....#..#...#.#.#.......#...#
..#..........#.................
.#.#.....#.#......#...#.....##.
.....#.#..##...##..#..###...#..
......#......#.#......#.##.....
#.#......#...#.......#....#....
..........#....#.#..#.....##...
#...........#.#....#.##....#.#.
#.#....#..#.........###....#...
..............#..##.......#....
.......................#.##.#..
##...............##....#..#.#..
.#.#..#.##...#.............#...
...#...........#............#..
..#......#........##....#.#.##.
.#.#..#........#....#....#....#
.#.....#.##....#.....#..#...#..
......#...#..........#..###....
..#.#.......#........#........#
.......##.####..........#......
.#.#..#......##..#.........#..#
..#...##.#.......#...#.##...#.#
#.#..........#..#.#.#..........
.....#......#............#.....
........###...#.......#........
.....#.##....#....#............
...#.#....##.....#.....#.......
..#.............#......#.......
.#....#...#....##..#......#....
..#.....#.#............#.......
......#........##.........#...#
.......#........#..#.#.#...##.#
#....#...#..#.......#....##....
#...##.#.#.....#.......#.......
.....#........#.#.....#...##...
..#....#..##......#.#.....#...#
....#.....#......#.....#.......
#.#....#......#...##...........
..#.......#...#...............#
........#........#.............
#.#.#......#...#..#..........#.
.##...#.........#........#..#..
#.#.#...#.#.......#.....#...#..
...#..............#..........#.
#.#...#.###.............#......
................#.....###.##.#.
.......#..........#....#..#....
......##....#..#..##...........
...#...#.....######.......#....
..##.....##.#...#.........#.#.#
.......#...#..#.#.#...........#
........###.............#...#.#
#.....#.........#.............#
..#...#.....#..................
.....#....#.....#......#.#....#
...#....#........##...#.......#
...##.#...#.....#..............
..#.##....##..#.........#......
.....#..#.#....#...#......#.#..
...........##..##...#..#..###..
#...........#.........####....#
.#...........#...........###...
........#................#.....
.....#....#............#....#.#
...#...#.......#...............
#.....##.#.......#.#...........
#.......#.#.#.#..#...#.........
....####.#...#.#......#.....##.
...##...#.....#.#......#..#....
..........#..#....#......###...
...................#....##...#.
....#......#........#...##..#..
##...#.........#.#......#......
#........#...#....#......#.....
#..#.......#...............##..
......##......#...........##.#.
......#..#....#....#.##........
..#....#..#.#.###....#.........
.#......#..#..............#....
.#..........#...#..#.#...#.....
....#......#..#......#....#....
...##.....#............####..#.
......#.#...#....#..#...#..#.#.
......##.......................
#.##........#...........####..#
.....#......#.......#.#....#...
#.......#....#.....#....#...##.
.....#..##.#...........#..#...#
...........#.##.#.#...#.#..#...
..#.......#.#....#..#..........
...#.......##..#.............#.
....#..#....#....#...#....#....
#......#.#...##..........#..#..
..#.#.......#.........#......#.
#...............#.............#
....##..#......................
.##....#............#......#...
.......#....#..##......##......
#..##.....#..#..........#......
...#.........#.......#..##.....
....#.##.....#.#...#...#.....#.
##...........#.#..#...#.#......
....#.............##...#.#..#..
...#....#......................
#..#...##.#.......#.##..#.###..
...##.#.#...##........##...#...
......##..#..#.....#..#.#..#...
#.......##...............##.#..
.##......#..#....#...##..#..#.#
##.........##..#...#.....##....
...#..........#...#..##.#......
..##.#........#...#..........##
.......................##.#....
....#...#...#..###.#.......#.##
....#....#.#..........#.##.....
..#..........##...#....#.......
.....#.....#.....#..#.........#
..##..##..#..#....#..#.......##
.............#............##...
....#.#.#.......###.........#..
...##.#..........#.#...#.#.....
.#........#..#.#.#..#..........
...##...#.....##.......#..#..#.
...#......#..#.......##.#.#....
.........#.........##........#.
.........##..................##
....##.....#................#..
....#..................##...#.#
.........#..............#......
...#......#..#..#....#..#...##.
.#.##......##...#.#......#.#...
...#.#...###....#...#.#..#....#
....#..#.......#.....#..##.#.#.
#.#.#..#.......#####.#..##..#..
#..........#.....#..#.#..#.....
.#......#...#..#.#..#..#.......
...#....##...#..........#.##.#.
#.##..#...#..................#.
......#.###..#..#..#.......#...
...#....#...#..#............###
#.........#........#.......#...
...#..#.................#....##
...#.#.............##......#...
##....#.##.............##......
#............#..#..#.....#.....
....#........#...#.....#.#...##
..#.##..#.....................#
#.#........#...#..#...#.#......
...#..#...........##.....#.....
......#.#....#..##...#.....#...
......#......#.###..##.........
....#.......#...##.##.....#....
#.....##....#........#..##.....
.#..#..#..#..#.#...#...#.......
.......##...#......#.........#.
.#..##....#.....#...........##.
.......##....#.#........#......
..#.#.#....#...................
.#...#.......#...#.#......#....
..##.##..##...........###......
#...#......#.......#...........
#....##.#.......#.........#....
.............##.#.#..#...#...#.
..##.##...........#.........##.
#.#...#..........#.#....#....##
.....#.....#..##..#............
#.........#.........#.#...##..#
...#.#.....#.........###..#....
..#.#.##.#...................#.
......####....#.......#.......#
.........#..#..#....#..##......
....#..........#...##........#.
..........#..#....#.....#....#.
.#.#.................#....#....
.......#......#.....#...##.....
....#..............#...........
###...........##.#...........#.
...####.......#...#....#.#...#.
..##.#................#........
...#..#....#.....#.....##..#...
##.#....#....##..........#.#..#
...#....#.....#................
..#...#....#..#..#.##.##..#....
....#....#.##.....#...#......#.
......#................#..#..#.
...##..#...#....#.#.....#..#...
#...#..............#.#.....#.#.
....#.........#.##...#.#....#..
..................#..##.#......
.#.....#.....#.............#..#
..........####....###..##...#..
......#........#...#......##..#
#......#.#..........#....#.#...
###................#.#....#....
#..#.##.#.............#..#.....
.....#............#.....##.....
....#.....#....#.........#.....
#..#...........###.#....#......
..#............##...#........##
..#....#..#....#.....#.......#.
..#..#.#.#.##.#..#...#.....#...
..........#..#.................
...#.#......#..##........#.....
...............#...............
#.......#.......#....#.........
#...........#....#.............
....#..#..........#....#..##...
.........#.#.#.........#......#
.....#...##.....#.#.......#...#
.........#....#...#.......#....

标签: pythonalgorithmmatrix

解决方案


Python 列表(和矩阵)是零索引的。

您太早地将 1 添加到该行,因此在最后一行中,您尝试访问最后一行之后不存在的行。尝试将row = row + 1放在col = col + 1. 然后从那里继续调试。

或者,您可以从 开始row = -1,它应该可以工作,但它有点“脏”。

此外,由于您已经在使用枚举,您可能需要考虑

for idx, i in enumerate(grid):
    grid[idx][col] == '#'

因为 enumerate 已经为您提供了所需的索引。


推荐阅读