Search Numpy array with multiple values

前端 未结 2 2093
北恋
北恋 2020-12-21 14:22

I have numpy 2d array having duplicate values.

I am searching the array like this.

In [104]: import numpy as np

In [105]: array = np.array

In [106]         


        
相关标签:
2条回答
  • 2020-12-21 15:02

    You can do

    a[numpy.in1d(a[:, 0], num_list), :]
    
    0 讨论(0)
  • 2020-12-21 15:04

    Approach #1 : Using np.in1d -

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

    Approach #2 : Using 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]]
    
    0 讨论(0)
提交回复
热议问题