簡體   English   中英

為什么在遍歷列表矩陣時出現列表索引超出范圍錯誤?

[英]Why am I getting a list index out of range error when traversing a list matrix?

我正在嘗試遍歷矩陣列表,即(向右移動 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 值,這個問題也會持續存在。 我的輸入數據在這里:

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

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

您太早地將 1 添加到該行,因此在最后一行中,您嘗試訪問最后一行之后不存在的行。 嘗試將row = row + 1放在col = col + 1之前。 然后從那里繼續調試。

或者,您可以從row = -1開始,它應該可以工作,但它有點“臟”。

此外,由於您已經在使用枚舉,您可能需要考慮

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

因為 enumerate 已經為您提供了所需的索引。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM