Find nearest value in numpy array

后端 未结 16 1612
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 10:18

Is there a numpy-thonic way, e.g. function, to find the nearest value in an array?

Example:

np.find_nearest( array, value )
16条回答
  •  无人及你
    2020-11-22 10:58

    If you don't want to use numpy this will do it:

    def find_nearest(array, value):
        n = [abs(i-value) for i in array]
        idx = n.index(min(n))
        return array[idx]
    

提交回复
热议问题