Numpy: find index of the elements within range

前端 未结 12 2285
盖世英雄少女心
盖世英雄少女心 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:06

    Wanted to add numexpr into the mix:

    import numpy as np
    import numexpr as ne
    
    a = np.array([1, 3, 5, 6, 9, 10, 14, 15, 56])  
    
    np.where(ne.evaluate("(6 <= a) & (a <= 10)"))[0]
    # array([3, 4, 5], dtype=int64)
    

    Would only make sense for larger arrays with millions... or if you hitting a memory limits.

提交回复
热议问题