Answers for "python sort to find the nearest of 0"

6

select closest number in array python

import numpy as np
def find_nearest(array, value):
    array = np.asarray(array)
    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
Posted by: Guest on March-15-2020
0

python find closest value in list to zero

# ------- FIND VALUE (AND INDEX) CLOSEST TO ZERO INSIDE A LIST ------- # 

myList = [23, -2, 20, 19, -10, 4]
min_val, idx = min([(abs(val), idx) for (idx, val) in enumerate(myList)])
Posted by: Guest on August-30-2020

Code answers related to "python sort to find the nearest of 0"

Python Answers by Framework

Browse Popular Code Answers by Language