numpy的:对于一个阵列的每个元素,找到另一个数组索引 [英] Numpy: For every element in one array, find the index in another array

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

问题描述

我有两个一维数组,X放大器; Y,其中一个比另一个小。我试图找到在x和y的每一个元素的索引。

I have two 1D arrays, x & y, one smaller than the other. I'm trying to find the index of every element of y in x.

我已经发现了两个天真的方式来做到这一点,首先是缓慢的,而第二个内存密集型。

I've found two naive ways to do this, the first is slow, and the second memory-intensive.

indices= []
for iy in y:
    indices += np.where(x==iy)[0][0]

的内存猪

xe = np.outer([1,]*len(x), y)
ye = np.outer(x, [1,]*len(y))
junk, indices = np.where(np.equal(xe, ye))

有一个更快的方法或更少的内存密集型的方法呢?理想情况下,搜索将充分利用这一事实,我们在一个列表搜索不是一回事,但很多事情,因而稍微更适合并行化。
奖励积分,如果你不认为y的每一个元素实际上是x中。

Is there a faster way or less memory intensive approach? Ideally the search would take advantage of the fact that we are searching for not one thing in a list, but many things, and thus is slightly more amenable to parallelization. Bonus points if you don't assume that every element of y is actually in x.

推荐答案

乔金顿说,的 searchsorted()可以非常快速地搜索元素。为了解决这个问题不是x中的元素,你可以检查与原始y的搜索结果中,并创建一个蒙面的数组:

As Joe Kington said, searchsorted() can search element very quickly. To deal with elements that are not in x, you can check the searched result with original y, and create a masked array:

import numpy as np
x = np.array([3,5,7,1,9,8,6,6])
y = np.array([2,1,5,10,100,6])

index = np.argsort(x)
sorted_x = x[index]
sorted_index = np.searchsorted(sorted_x, y)

yindex = np.take(index, sorted_index, mode="clip")
mask = x[yindex] != y

result = np.ma.array(yindex, mask=mask)
print result

结果是:

[-- 3 1 -- -- 6]

这篇关于numpy的:对于一个阵列的每个元素,找到另一个数组索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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