列表索引越界的原因不是显而易见的numpy

2024-09-27 04:19:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我不知道为什么,但是我一直得到这个错误,即使列表索引没有超过索引的数目。获取此错误的代码如下:

    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时,就会出现错误。如果我把循环改成:

^{pr2}$

然后它到达了范围的末尾(在本例中是329)。在

怎么解决这个问题呢?如果可能的话,我不希望嵌套这个循环,并且必须拆分每个数组的大小,例如%max==300

如有任何指导,我们将不胜感激

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

顶点法线索引的最后8个索引:(因此,获取每行的第一个编号,例如278195281)

enter image description here


Tags: 代码add列表错误np数组arrayprint
1条回答
网友
1楼 · 发布于 2024-09-27 04:19:17

根据您的评论和回溯,错误在这一行:
nF1 = vecNormals[vertextNormalIndices[f][0][0]]

因此,错误必须vertextNormalIndices[519]或{}是空列表-请尝试在循环中打印出它们。在

作为旁白:

{a1'要直接遍历元素的索引,还需要使用

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])

相关问题 更多 >

    热门问题