【问题标题】:Getting indices of elements that are in another list in numpy在numpy中获取另一个列表中的元素的索引
【发布时间】:2015-01-07 19:59:44
【问题描述】:

我有两个 numpy 数组,我想获取第二个数组中第一个数组中所有元素的索引。例如:

import numpy as np
x = np.array([0,1,1,2,3,4,5,5])
y = np.array([1,3])
# want to get np.array([1,2,4]) 

如果y 是一个标量,我可以只做np.where(x == y)。值数组是否有等价物?

【问题讨论】:

    标签: python arrays numpy


    【解决方案1】:

    你可以numpy.wherenumpy.in1d:

    >>> np.where(np.in1d(x, y))
    (array([1, 2, 4]),)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-24
      • 1970-01-01
      • 1970-01-01
      • 2013-12-03
      • 2015-11-18
      相关资源
      最近更新 更多