How to check efficiently numpy array contains item within given range?

前端 未结 2 1629
旧巷少年郎
旧巷少年郎 2021-01-22 02:47

I have a numpy array, called a , I want to check whether it contains an item in a range, specified by two values.

import numpy as np
a = np.arange(1         


        
2条回答
  •  梦谈多话
    2021-01-22 03:38

    Numpy arrays doesn't work well with pythonic a < x < b. But there's func for this:

    np.logical_and(a > mintrshold, a < maxtreshold)
    

    or

    np.logical_and(a > mintrshold, a < maxtreshold).any()
    

    in your particular case. Basically, you should combine two element-wise ops. Look for logic funcs for more details

提交回复
热议问题