I am not certain but a np.searchsorted could be doing what you want here in one line.