(array, value, sorted_1d=True)
return idx, array[idx]
Find the nearest value to a reference value and its index in an array.
Find the nearest value to a reference value and its index in an array.
Find the nearest value to a reference value and its index in an array .
def find_nearest(array, value, sorted_1d=True):
    """Find the nearest value to a reference value and its index in an array."""
    if not sorted_1d:
        array = np.sort(array)
    idx = np.searchsorted(array, value, side='left')
    if idx > 0 and (idx == len(array) or np.abs(value - array[idx - 1]) < np.abs(value - array[idx])):
        idx -= 1
    return idx, array[idx]
def find_nearest ( array , value , sorted_1d = True ) : if not sorted_1d : array = np . sort ( array ) idx = np . searchsorted ( array , value , side = 'left' ) if idx > 0 and ( idx == len ( array ) or np . abs ( value - array [ idx - 1 ] ) < np . abs ( value - array [ idx ] ) ) : idx -= 1 return idx , array [ idx ]