首页 > 解决方案 > 恼人的 Python 列表索引超出范围错误

问题描述

我有一个数组已经初始化为 3x6 数组,名为self.ascii. 我正在尝试使用代码一次更改此数组的 3x3 块:

for x in range(3):
    for y in range(3):
       # currY and currX represent the bottom left corner of the 3x3 square I am tyring to alter
       self.ascii[y+currY][x+currX] = arr[y][x] # yes I know the x and y values are backwards between the arrays

但是我在 GNURadio-Companion 中不断收到错误消息list index out of range——甚至我尝试运行它之前。我知道这里的索引没有离开列表的范围。

问题:为什么 python 在运行之前会检查这个,我应该以不同的方式加载这些 3x3 数组

标签: pythonlistgnuradio-companion

解决方案


学会使用pdb

import pdb

pdb.set_trace()

# Now your code.
for x in range(3):
    for y in range(3):
       self.ascii[y+currY][x+currX] = arr[y][x]

这将使您进入解释器,您可以在其中查看您喜欢的任何内容的值,并使用诸如n执行代码中的下一条指令之类的命令。一步一步地去,直到你找到哪个索引超出了它的列表范围。


推荐阅读