我需要找到一个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]),)