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 :: python find item in list 
Python :: set header in dataframe 2nd line 
Python :: reportlab page size a4 
Python :: sort a list numbers in python 
Python :: create a generator from a list 
Python :: python read in integers separated by spaces 
Python :: render template in django 
Python :: how to take input in python 
Python :: python create env ubuntu 
Python :: sieve of eratosthenes python 
Python :: append path to sys jupyter notebook 
Python :: get current data with python 
Python :: convert string of list to list python 
Python :: python anagram finder 
Python :: get only every 2 rows pandas 
Python :: flask error 
Python :: Extract column from a pandas dataframe 
Python :: how to capitalize the first letter in a list python 
Python :: python code to receive gmail 
Python :: Get all the numerical column from the dataframe using python 
Python :: remove hyperlink from text python 
Python :: print even numbers in python 
Python :: python define random variables name 
Python :: does jupyter notebook need internet 
Python :: python mean 
Python :: print 1to 10 number without using loop in python 
Python :: plot second axis plotly 
Python :: softmax function python 
Python :: pandas lamda column reference 
Python :: numpy sort 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =