Numpy int array: Find indices of multiple target ints

后端 未结 3 471
孤独总比滥情好
孤独总比滥情好 2021-01-18 07:46

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(         


        
相关标签:
3条回答
  • 2021-01-18 08:23

    I would say using np.in1d would be the intuitive solution to solve such a case. Having said that, based on this solution here's an alternative with np.searchsorted -

    sidx = np.argsort(searchvals)
    left_idx = np.searchsorted(searchvals,values,sorter=sidx,side='left')
    right_idx = np.searchsorted(searchvals,values,sorter=sidx,side='right')
    out = np.where(left_idx != right_idx)[0]
    
    0 讨论(0)
  • 2021-01-18 08:26

    Can you avoid numpy all together? List concatenation should be much faster than relying on numpy's methods. This will still work even if values needs to be a numpy array.

    result = []
    for sv in searchvals:
        result += [i for i in range(len(values)) if values[i] == sv]
    
    0 讨论(0)
  • 2021-01-18 08:30

    Is this fast enough?

    >>> np.where(np.in1d(values, searchvals))
    (array([ 0,  2,  3,  8, 10]),)
    
    0 讨论(0)
提交回复
热议问题