【发布时间】:2018-11-05 11:33:14
【问题描述】:
假设我们有两个简单的 1d numpy 数组:
a = np.array([1, 1, 2, 5, 7, 8, 2, 4, 5, 6])
b = np.array([1, 5, 7])
现在我想要的是获取所有可能的索引,其中数组 b 中的每个值都包含在数组 a 中。
我们可以做例如以下:
idx = np.where(np.any(a.reshape((-1, 1))==b, axis=1))[0]
idx 是 array([0, 1, 3, 4, 8])(这是我真正想要的)。
现在我真的很好奇在 numpy 或任何其他库中是否已经有类似的函数来解决这个问题(我相信已经存在一个)。否则我会坚持现在的工作方法。
【问题讨论】:
-
使用
np.where(np.in1d(a, b))[0]。
标签: python arrays numpy indexof