Created
June 28, 2016 08:55
-
-
Save quarbby/f880476cad235c90f634bd389cefb162 to your computer and use it in GitHub Desktop.
Python find nearest float
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def find_nearest(array,value): | |
idx = (np.abs(array-value)).argmin() | |
return array[idx] | |
array = np.random.random(10) | |
print(array) | |
# [ 0.21069679 0.61290182 0.63425412 0.84635244 0.91599191 0.00213826 | |
# 0.17104965 0.56874386 0.57319379 0.28719469] | |
value = 0.5 | |
print(find_nearest(array, value)) | |
# 0.568743859261 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment