Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

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)   




Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #find #closest #position #time #list #python
ADD COMMENT
Topic
Name
4+4 =