Numpy int数组:查找多个目标int的索引 [英] Numpy int array: Find indices of multiple target ints

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

问题描述

我有一个大的numpy数组(dtype=int)和一组我想在该数组中找到的数字,例如

I have a large numpy array (dtype=int) and a set of numbers which I'd like to find in that array, e.g.,

import numpy as np
values = np.array([1, 2, 3, 1, 2, 4, 5, 6, 3, 2, 1])
searchvals = [3, 1]
# result = [0, 2, 3, 8, 10]

result数组不必排序.

速度是一个问题,并且由于valuessearchvals都可能很大,

Speed is an issue, and since both values and searchvals can be large,

for searchval in searchvals:
    np.where(values == searchval)[0]

不剪掉.

有任何提示吗?

推荐答案

这样够快吗?

>>> np.where(np.in1d(values, searchvals))
(array([ 0,  2,  3,  8, 10]),)

这篇关于Numpy int数组:查找多个目标int的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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