find the closest position by time list python

40

find the closest position by time list python -


a = [13.6, 13.0, 12.1, 11.3, 10.3, 9.0, 8.8, 8.1]   #list
value = 11.5										#value to find

min(range(len(a)), key=lambda i: abs(a[i]- value))	#just index 
#result: 3
    
min(enumerate(a), key=lambda x: abs(x[1]-value)) 	#index and value
#result: (3, 11.3)   




how to get index of closest value in list python -

min(range(len(a)), key=lambda i: abs(a[i]-11.5))

python find index of closest value in list -

min(range(len(a)), key=lambda i: abs(a[i]-11.5))

Comments

Submit
0 Comments