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

前端 未结 2 1603
旧巷少年郎
旧巷少年郎 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-22 03:26

    Adding to the pure Numpy answer we can also use itertools

    import itertools
    
    bool(list(itertools.ifilter(lambda x: 33 <= x <= 66, a)))
    

    For smaller arrays this would suffice:

    bool(filter(lambda x: 33 <= x <= 66, a))
    

提交回复
热议问题