Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用多个值搜索Numpy数组_Python_Arrays_Numpy - Fatal编程技术网

Python 使用多个值搜索Numpy数组

Python 使用多个值搜索Numpy数组,python,arrays,numpy,Python,Arrays,Numpy,我有一个具有重复值的numpy 2d数组 我正在像这样搜索数组 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],

我有一个具有重复值的numpy 2d数组

我正在像这样搜索数组

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]]
输入是具有类似于列0值的数字的列表。 我想要的最终结果是以任何格式生成的行,例如数组、列表或元组

array([[1, 2, 3],
       [1, 2, 3],
       [4, 8, 9],
       [4, 2, 3],
       [5, 2, 3]])
我的代码工作得很好,但看起来不太像Python。有没有更好的多值搜索策略

a[np.where(a[:,0]==l)]
那样,只有一次查找才能得到所有值

我的实际数组很大

方法#1:使用-

方法#2:使用-

你能行

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

对不起,解释得太久了。我在代码审查上发布了类似的问题,但我没有正确解释。很抱歉将变量l更改为num_列表。您认为哪一个更好?我正在为我的工作做评估case@Scripting.FileSystemObject如果在
num_list
中有大量元素,我想说
searchsorted
值得一试。
0.60s vs 0.32s对于inad v searchsorted
方法2对我的情况没有用。我不知道,但它没有提供所有的搜索。我对numpy语法是新手。最后是什么?它的意思是“拿走一切”。所以
a[0,:]
是第一行,
a[:,0]
是第一列,等等。迪瓦卡在回答中说:我感到困惑。
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), :]