Search
 
SCRIPT & CODE EXAMPLE
 

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)   




Comment

python find closest value in list

def closest(lst, K):
      
    return lst[min(range(len(lst)), key = lambda i: abs(lst[i]-K))]
      
# Driver code
lst = [3.64, 5.2, 9.42, 9.35, 8.5, 8]
K = 9.1
print(closest(lst, K))
Comment

how to get index of closest value in list python

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

python find index of closest value in list

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

PREVIOUS NEXT
Code Example
Python :: check cuda version pytorch 
Python :: no python 3.10 installation was detected 
Python :: prettytable python 
Python :: python install required packages 
Python :: format integer to be money python 
Python :: sklearn minmaxscaler pandas 
Python :: tkinter load image 
Python :: human readable time difference python 
Python :: save numpy array to csv 
Python :: python diamond print 
Python :: python opencv write text on image 
Python :: sum of all nan values pandas 
Python :: python print colored text 
Python :: string of numbers to list of integers python 
Python :: python print float with 2 decimals 
Python :: filter with different operator in django 
Python :: how to pause code for some time in python 
Python :: how to convert a list into a dataframe in python 
Python :: how to remove coma in python 
Python :: pandas plot xlabel 
Python :: python get copied text 
Python :: python pandas apply to one column 
Python :: get current month py 
Python :: converting a csv into python list 
Python :: extract frames from video python 
Python :: selenium python switch to iframe 
Python :: AssertionError: Relational field must provide a `queryset` argument, override `get_queryset`, or set read_only=`True` 
Python :: for e in p.event.get(): pygame.error: video system not initialized 
Python :: how to read csv file online into pandas 
Python :: python day from date 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =