Python ';索引器:只有整数、片(`:`)、省略号(`…`)和#x27。。。?
这个错误有很多问题,但我一直无法在代码中找到问题的根源。我的代码如下:Python ';索引器:只有整数、片(`:`)、省略号(`…`)和#x27。。。?,python,numpy,index-error,Python,Numpy,Index Error,这个错误有很多问题,但我一直无法在代码中找到问题的根源。我的代码如下: for i in segs: if relDiff(segs[i+1], segs[i]) > 0.05: arr_x[i] = 0; arr_y[i] = 0 我在第三行得到错误:索引器:只有整数、片(:)、省略号(…)、numpy.newaxis(None)和整数或布尔数组是有效的索引 segs是一个数组,relDiff是我制作的一个函数,用于计算相对差异。以下是该函数: def relD
for i in segs:
if relDiff(segs[i+1], segs[i]) > 0.05:
arr_x[i] = 0; arr_y[i] = 0
我在第三行得到错误:索引器:只有整数、片(
:)、省略号(
…)、numpy.newaxis(
None)和整数或布尔数组是有效的索引
segs
是一个数组,relDiff
是我制作的一个函数,用于计算相对差异。以下是该函数:
def relDiff(x,x_ref):
return np.abs((x-x_ref)/x_ref)
非常感谢您的帮助 由于使用
segs
中的项作为arr\u x
和arr\u y
的索引,segs
必须是整数的列表/数组。否则,您必须将每个项转换为整数,如arr\ux[int(i)]
。在您的程序中这样做是否合乎逻辑取决于您自己,因为您的需求是未知的。我们不知道segs
包含什么。因为您使用segs
中的项作为arr\u x
和arr\u y
的索引,segs
必须是整数的列表/数组。否则,您必须将每个项转换为整数,如arr\ux[int(i)]
。在您的程序中这样做是否合乎逻辑取决于您自己,因为您的需求是未知的。我们不知道segs包含什么。试试这个:
for i in segs:
print(i)
segs[i]
问题应该变得显而易见
In [317]: x = np.array([1.2, 3.2, .5])
In [318]: for i in x:
...: print(i)
...: x[i]
...:
1.2
Traceback (most recent call last):
File "<ipython-input-318-41c2ba7b6f7d>", line 3, in <module>
x[i]
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
In [319]: x[1.2]
Traceback (most recent call last):
File "<ipython-input-319-28ffb042b45f>", line 1, in <module>
x[1.2]
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
试试这个:
for i in segs:
print(i)
segs[i]
问题应该变得显而易见
In [317]: x = np.array([1.2, 3.2, .5])
In [318]: for i in x:
...: print(i)
...: x[i]
...:
1.2
Traceback (most recent call last):
File "<ipython-input-318-41c2ba7b6f7d>", line 3, in <module>
x[i]
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
In [319]: x[1.2]
Traceback (most recent call last):
File "<ipython-input-319-28ffb042b45f>", line 1, in <module>
x[1.2]
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
您是否花时间查看了循环中的
i
?将segs
索引为其自身的元素有意义吗?您是否花时间查看循环中的i
?将segs
索引为其自身的元素有意义吗?