Use numpy argmax to find the first occurrence of value greater than 5 in the following array: arr = range(2, 20)

286 Views Asked by At

Use numpy argmax to find the first occurrence of value greater than 5 in the following array:

arr = range(2, 20)
1

There are 1 best solutions below

0
Xynias On
import numpy as np

arr = np.array(range(2, 20))
idx = np.argmax(np.concatenate((arr[arr <= 5], [arr[arr > 5][0]])))

or

import numpy as np

arr = np.array(range(2, 20))
idx = np.argmax(arr[arr <= 5]) + 1