Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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
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

PREVIOUS NEXT
Code Example
Python :: colab cuda version 
Python :: how to plot count on column of dataframe 
Python :: python pil image flip 
Python :: werkzeug.datastructures.filestorage to numpy 
Python :: python duplicate file 
Python :: exception get line number python 
Python :: isprime function in python 
Python :: clear console python 
Python :: python infinite value 
Python :: pandas columns starting with 
Python :: python key down 
Python :: arrondi supérieur python 
Python :: python random 
Python :: Find the Runner Up Score solution in python3 
Python :: how to import image in python 
Python :: f string round 
Python :: how to change background color in python turtle 
Python :: python word cloud 
Python :: python get file extension from path 
Python :: extract first letter of column python 
Python :: extract numbers from string python 
Python :: python radians to degrees 
Python :: dollar 
Python :: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools. 
Python :: pyqt5 change button color 
Python :: check if number is power of 2 python 
Python :: django runserver 
Python :: python setup.py bdist_wheel did not run successfully 
Python :: Print Table Using While Loop In Python 
Python :: business logic in django 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =