We can assume that the full range of year
consists of consecutive years. With that in mind, we can just subtract the valid years by the minimum year in the range and get the desired result.
>>> valid = np.array([1999, 2005, 2007])
>>> year_range = np.array([1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007])
Subtracting by the minimum year gives the result. You may also need np.where
to make sure the indices do not exceed the length of the year range.
>>> valid - np.min(year_range)
array([ 0, 6, 8])
There you go, no loops or search functions. But to be honest, by subtracting the array and by using np.where
, Numpy does perform loops behind the scenes. There is no actual way to avoid loops in this situation.