numpy where function TypeError



我需要找到一个2D数组的索引,其中第一列等于0.1。像这样:

Data = [[0, 1], [0.075, 1], [0.1, 1], [0.11, 1], [0.125, 1]]
print(np.where(Data[:,0] == 0.1))

但是我得到以下错误:

TypeError: list indices must be integers or slices, not tuple

如果有人能帮我就太好了:)

只是一个打字错误…你没有将Data初始化为array,它是list形式

代码:

import numpy as np
Data = np.array([[0, 1], [0.075, 1], [0.1, 1], [0.11, 1], [0.125, 1]])
print(np.where(Data[:,0] == 0.1))

输出: -

(array([2]),)

错误:

TypeError: list indices must be integers or slices, not tuple

是因为Data是一个列表而不是numpy数组,注意它说元组是因为您将元组(:, 0)传递给Data__getitem__方法。要了解更多信息,请参阅此,要修复它只需执行:

import numpy as np
Data = np.array([[0, 1], [0.075, 1], [0.1, 1], [0.11, 1], [0.125, 1]])
print(np.where(Data[:, 0] == 0.1))

(array([2]),)

相关内容

最新更新