python - 恼人的 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 数组
解决方案
学会使用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
执行代码中的下一条指令之类的命令。一步一步地去,直到你找到哪个索引超出了它的列表范围。
推荐阅读
- delphi - 在 Delphi Rio 10.3 中以时间格式摆脱 AM/PM(仅从 12:00 PM 到 12:00)
- javascript - 如何使此自定义按钮组件可跨不同控件重用?
- assembly - 如何将 MIPS ADD 1 / SLT / BNE 简化为更少的指令?
- r - 即使图表中未显示类别,也显示整个图例
- c++ - 尽管控制台显示没有错误,但应用程序错误?
- reactjs - redux-thunk - 即使我使用了 mapDispatchToProps 也没有调度
- sorting - 按第一列 Julia 对矩阵进行排序
- reactjs - 使 VSCode 将语法高亮应用于 .tsx 文件
- c - 将指向具有 volatile 成员的结构的指针作为函数参数传递
- cordova - 应用内浏览器未打开外部链接