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(100)
mintrshold=33
maxtreshold=66
My solution:
goodItems = np.zeros_like(a)
goodItems[(a<maxtreshold) & (a>mintrshold)] = 1
if goodItems.any():
print (there s an item within range)
Can you suggest me a more effective, pythonic way?
anyif you expect to find a valid item early in the array - being able to returnTruewithout iterating through the whole array can save a lot of runtime. Use a numpy expression with.any()if you don't expect an item early or at all. Always benchmark with real data.