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

问题描述

我正在寻找一个numpy函数来查找在向量(xs)中找到某些值的索引.这些值在另一个数组(ys)中给出.返回的索引必须遵循ys的顺序.

I am looking for a numpy function to find the indices at which certain values are found within a vector (xs). The values are given in another array (ys). The returned indices must follow the order of ys.

在代码中,我想用numpy函数替换下面的列表理解.

In code, I want to replace the list comprehension below by a numpy function.

>> import numpy as np
>> xs = np.asarray([45, 67, 32, 52, 94, 64, 21])
>> ys = np.asarray([67, 94])
>> ndx = np.asarray([np.nonzero(xs == y)[0][0] for y in ys]) # <---- This line
>> print(ndx)
[1 4]

有没有快速的方法?

谢谢

推荐答案

对于大型数组xsys,您需要更改基本方法以使其变得更快.如果您对xs进行排序很好,那么一个简单的选择就是使用numpy.searchsorted():

For big arrays xs and ys, you would need to change the basic approach for this to become fast. If you are fine with sorting xs, then an easy option is to use numpy.searchsorted():

xs.sort()
ndx = numpy.searchsorted(xs, ys)

如果保持xs的原始顺序很重要,您也可以使用这种方法,但是您需要记住原始索引:

If it is important to keep the original order of xs, you can use this approach, too, but you need to remember the original indices:

orig_indices = xs.argsort()
ndx = orig_indices[numpy.searchsorted(xs[orig_indices], ys)]

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

08-14 23:01