我不知道为什么,但我不断收到此错误,即使列表索引不超过索引数。出现此错误的代码如下:
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
任何指导将不胜感激
vertexNormalIndices 的最后 8 个索引:(因此,获取每行的第一个数字,例如 278、195、281)
最佳答案
根据您的评论,并查看您的回溯,错误位于此行:
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/