简体   繁体   English

使用多个值搜索Numpy数组

[英]Search Numpy array with multiple values

I have numpy 2d array having duplicate values. 我有numpy 2d数组有重复值。

I am searching the array like this. 我正在搜索这样的数组。

In [104]: import numpy as np

In [105]: array = np.array

In [106]: a = array([[1, 2, 3],
     ...:            [1, 2, 3],
     ...:            [2, 5, 6],
     ...:            [3, 8, 9],
     ...:            [4, 8, 9],
     ...:            [4, 2, 3],
     ...:            [5, 2, 3])

In [107]: num_list = [1, 4, 5]

In [108]: for i in num_list :
     ...:     print(a[np.where(a[:,0] == num_list)])
     ...:
 [[1 2 3]
 [1 2 3]]
[[4 8 9]
 [4 2 3]]
[[5 2 3]]

The input is list having number similar to column 0 values. 输入是具有与列0值类似的数字的列表。 The end result I want is the resulting rows in any format like array, list or tuple for example 我想要的最终结果是任何格式的结果行,例如array,list或tuple

array([[1, 2, 3],
       [1, 2, 3],
       [4, 8, 9],
       [4, 2, 3],
       [5, 2, 3]])

My code works fine but doesn't seem pythonic. 我的代码工作正常,但似乎并不像pythonic。 Is there any better searching strategy with multiple values? 有多个值的搜索策略是否更好?

like a[np.where(a[:,0] == l)] where only one time lookup is done to get all the values. 比如a[np.where(a[:,0] == l)] ,其中只进行一次查找以获取所有值。

my real array is large 我真正的阵列很大

Approach #1 : Using np.in1d - 方法#1:使用np.in1d -

a[np.in1d(a[:,0], num_list)]

Approach #2 : Using np.searchsorted - 方法#2:使用np.searchsorted -

num_arr = np.sort(num_list) # Sort num_list and get as array

# Get indices of occurrences of first column in num_list
idx = np.searchsorted(num_arr, a[:,0])

# Take care of out of bounds cases
idx[idx==len(num_arr)] = 0 

out = a[a[:,0] == num_arr[idx]]

你可以做

a[numpy.in1d(a[:, 0], num_list), :]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM