Python 多维numpy数组__
我有一个二维的Python 多维numpy数组__,python,arrays,numpy,Python,Arrays,Numpy,我有一个二维的np.数组 x = np.array([[1,2], [4,5], [4,6], [5,4], [4,5]]) 现在我想要x等于[4,5](->[1,4])的索引。操作员的工作方式不同: x == [4,5] array([[False, False], [ True, True], [ True, False], [False, False], [ True, True]], dtype=bool) 但是我想要像[F
np.数组
x = np.array([[1,2], [4,5], [4,6], [5,4], [4,5]])
现在我想要x等于[4,5]
(->[1,4]
)的索引。操作员的工作方式不同:
x == [4,5]
array([[False, False],
[ True, True],
[ True, False],
[False, False],
[ True, True]], dtype=bool)
但是我想要像[False,True,False,False,True]
这样的东西。执行和可以吗
通常阵列非常大,我必须多次这样做,因此我需要一种非常快速的方法。以前没有使用numpy的经验,但这适用于标准阵列:
x = [[1, 2], [4, 5], [4, 6], [5, 4], [4, 5]]
indices = [i for i, v in enumerate(x) if v == [4, 5]]
# gives [1, 4]
matches = [v == [4, 5] for v in x]
# gives [False, True, False, False, True]
以前没有使用numpy的经验,但这适用于标准阵列:
x = [[1, 2], [4, 5], [4, 6], [5, 4], [4, 5]]
indices = [i for i, v in enumerate(x) if v == [4, 5]]
# gives [1, 4]
matches = [v == [4, 5] for v in x]
# gives [False, True, False, False, True]
这应该是最基本的方式:
x = np.array([[1,2], [4,5], [4,6], [5,4], [4,5]])
(x == [4,5]).all(1)
#out: array([False, True, False, False, True], dtype=bool)
这应该是最基本的方式:
x = np.array([[1,2], [4,5], [4,6], [5,4], [4,5]])
(x == [4,5]).all(1)
#out: array([False, True, False, False, True], dtype=bool)
对不起,我忘了提我想要一个numpy解决方案。对不起,我忘了提我想要一个numpy解决方案