I am trying to code a function where it checks if the Numbers elements are between the Formating values. So for the first element of Numbers 3 is not between the first and the second element 0, 2 of Formating so it returns 0 as the result. For the elements 3,4 in Numbers are between 2 ,5 so it return those 3,4 as the numbers between 2,5. The index value keeps on getting incremented until the end of the Formating array. How could I fix the function below so it gives the expected output, without a for loop.
Numbers = np.array([3, 4, 5, 7, 8, 10,20])
Formating = np.array([0, 2 , 5, 12, 15, 22])
#previouse and curent index values
prevs = np.arange(0,len(Formating)-1,1)
currents = np.arange(1,len(Formating),1)
index = 0
np.where(Formating[prevs[index]] <= Numbers < Formating[currents[index]], Numbers, ++index)
Expected Output:
Numbers between 0,2 = 0
Numbers between 2,5 = 3,4
Numbers between 5,12 = 5,7,8,10
Numbers between 12,15 = 0
Numbers between 15,22 = 20
np.histogram(Numbers, bins=Formating)ornp.digitize(Numbers, Formating)?formattingrefers to converting numbers to strings, as in%d%3` or{}.format(3). Your use ofFormatingas an array, as though it means something, is misleading, and may discourage useful answers.Numbersisn't that great of a variable either (it looks more like a class name).