Numpy: find index of the elements within range

前端 未结 12 2233
盖世英雄少女心
盖世英雄少女心 2020-11-28 23:01

I have a numpy array of numbers, for example,

a = np.array([1, 3, 5, 6, 9, 10, 14, 15, 56])  

I would like to find all the indexes of the

12条回答
  •  失恋的感觉
    2020-11-28 23:23

    I thought I would add this because the a in the example you gave is sorted:

    import numpy as np
    a = [1, 3, 5, 6, 9, 10, 14, 15, 56] 
    start = np.searchsorted(a, 6, 'left')
    end = np.searchsorted(a, 10, 'right')
    rng = np.arange(start, end)
    rng
    # array([3, 4, 5])
    

提交回复
热议问题