python - 列表索引越界,原因不是很明显 numpy

标签 python arrays python-2.7 numpy

我不知道为什么,但我不断收到此错误,即使列表索引不超过索引数。出现此错误的代码如下:

    normalisedFaces = np.array([])
    for f in range(len(vertextNormalIndices)):
        nF1 = vecNormals[vertextNormalIndices[f][0][0]]
        nF2 = vecNormals[vertextNormalIndices[f][1][0]]
        nF3 = vecNormals[vertextNormalIndices[f][2][0]]
        normalisedFaces = np.hstack((normalisedFaces,(np.add(nF1,np.add(nF2,nF3))/ 3)))
        print(f)
    time.sleep(3)
    print(normalisedFaces[f])

我唯一的猜测是我已经达到了数组最大大小的末尾(?)对于这个例子,循环的范围是 529 ,但是当我达到 519 时就会出现错误。如果我更改循环到类似的内容:

    for f in range(len(vertextNormalIndices)-200):

然后它到达范围的末尾(因此,在本例中:329)。

如何解决这个问题?如果可能的话,我宁愿不必嵌套此循环,而必须将每个数组的大小分割为例如%最大==300

任何指导将不胜感激

我在这里附上了错误的屏幕截图: enter image description here

vertexNormalIndices 的最后 8 个索引:(因此,获取每行的第一个数字,例如 278、195、281)

enter image description here

最佳答案

根据您的评论,并查看您的回溯,错误位于此行:
nF1 = vecNormals[vertexNormalIndices[f][0][0]]

因此,错误必须vertextNormalIndices[519]vertextNormalIndices[519][0] 是一个空列表 - 尝试在循环中打印它们。

顺便说一句:

迭代列表的“Pythonic”方式是直接执行,如果您还需要获取每个元素的索引,则应该使用 enumerate :

normalisedFaces = np.array([])
for f, vertexNormalIndex in enumerate(vertextNormalIndices):
    nF1 = vecNormals[vertextNormalIndex[0][0]]
    nF2 = vecNormals[vertextNormalIndex[1][0]]
    nF3 = vecNormals[vertextNormalIndex[2][0]]
    normalisedFaces = np.hstack((normalisedFaces,(np.add(nF1,np.add(nF2,nF3))/ 3)))
    print(f)
time.sleep(3)
print(normalisedFaces[f])

关于python - 列表索引越界,原因不是很明显 numpy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37026746/

相关文章:

PythonAnywhere:尝试使用 easygui 模块时出现 Tkinter.py 错误

python - 尝试从 python 中的字典中拆分作为键的字符串,出现无法解释的错误。为什么?

arrays - 从元组数组中删除元组

python-2.7 - 从 Popen 内部(扭曲的) timer.LoopingCall() 运行 ffmpeg

python - 无法在 Python 2.7 上安装 pip

Python:从类内部访问静态类变量

python - 如何在 Arch Linux 中安装 Python 3.8 和 Python 3.9?

c++ - Arduino 传递多维数组

javascript - 为什么 GSON 将对象数组转换为字符串数组?

python-2.7 - 使用boto将目录上传到s3