在numpy数组中查找值列表的索引 [英] Find indices of a list of values in a numpy array

查看:437
本文介绍了在numpy数组中查找值列表的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个numpy主数组.给定另一个具有重复元素的搜索值数组,我想在主数组中生成这些搜索值的索引.

I have a numpy master array. Given another array of search values, with repeating elements, I want to produce the indices of these search values in the master array.

例如:主数组为[1,2,3,4,5],搜索数组为[4,2,2,3]

E.g.: master array is [1,2,3,4,5], search array is [4,2,2,3]

解决方案:[3,1,1,2]

Solution: [3,1,1,2]

是否有一个本机" numpy函数可以有效地执行此操作(意味着以C速度而不是python速度)?

Is there a "native" numpy function that does this efficiently (meaning at C speed, rather than python speed)?

我知道以下解决方案,但首先,它是一个python列表理解,其次,它将搜索2的索引两次.

I'm aware of the following solution, but, first, it's a python list comprehension, and second, it'll search for the index of 2 twice.

ma = np.array([1,2,3,4,5])
sl = np.array([4,2,2,3])
ans = [np.where(ma==i) for i in sl]

此外,如果我不得不求助于排序和二进制搜索,我将把它作为最后的手段(双关语不适合所有级别).我有兴趣查找我是否在numpy库中缺少一些基本知识.这些列表非常大,因此性能至关重要.

Also, if I have to resort to sorting and binary search, I will do it as a last resort (puns not intended at all sorts of levels). I am interested in finding if I'm missing something basic from the numpy library. These lists are very large, so performance is paramount.

谢谢.

发布之前,我尝试了以下令人沮丧的结果:

Before posting I'd tried the following with dismal results:

[np.searchsorted(ma,x) for x in sl]

@pierre发布的解决方案性能更高,而且正是我想要的.

The solution posted by @pierre is much more performant and exactly what I was looking for.

推荐答案

np.searchsorted会为您工作吗?

>>> master = np.array([1,2,3,4,5])
>>> search = np.array([4,2,2,3])
>>> np.searchsorted(master, search)
array([3, 1, 1, 2])

这篇关于在numpy数组中查找值列表的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆