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

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 closest value to n in list

def closest(arr, target):
  #1: calculate the delta between the number and the target for each numer (this is done in the list comprehension)
  #2: enumerate them to get the index of the minimum
  #2.1: key=... is used because now we have a tuple (check enumerate docs) so, knowing that x[0] is the index, we chose x[1], which is the actual value
  #3 once we have the index use it to access the correct value in the original array
  return arr[min(enumerate([abs(target-x) for x in arr]), key=lambda x: x[1])[0]]


def closest_with_bias(arr, target):
  # this time we will do the same thing but we will have a bias towards numbers above or below the target
  # for example if target is 0 then we can favor positive or negative numbers based on the POSITIVE_BIAS value
  # differences:
  # in the key function we add POSITIVE_BIAS to the calculated delta after checking if x is greater or equal than the target
  # example:
    # arr = [-4, 4, 5]
    # target = 0
    # calculated delta array = [4.1, 4, 5]
    # min will then find the 4 which has index 1
    # we have now favored 4 instead of -4
  POSITIVE_BIAS = .1
  bias = lambda x: abs(target-x[1]) if x[1]>=target else abs(target-x[1])+POSITIVE_BIAS
  return arr[min(enumerate([bias(x) for x in arr]), key=bias)[0]]
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

python get nearest value in list

def takeClosest(num,collection):
   return min(collection,key=lambda x:abs(x-num))
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 :: extract filename from path in python 
Python :: how to make a latency command discord.py 
Python :: discord py get user by id 
Python :: list to excel python 
Python :: hotkey python 
Python :: python flask mail 
Python :: drf default pagination 
Python :: how to only print final iteration of a for loop pyhton 
Python :: python append element to array 
Python :: pandas iterate over a series 
Python :: unnamed 0 pandas 
Python :: if keyboard.is_pressed 
Python :: file handling modes in python 
Python :: convert a tuple into string python 
Python :: check if back is pressed python 
Python :: python selenium clear input 
Python :: flask redirect to url 
Python :: python inf 
Python :: create python file kali linux 
Python :: dice rolling simulator python 
Python :: Creating a list with list comprehensions 
Python :: python get duration of wav file 
Python :: plot histogram python 
Python :: drop row pandas 
Python :: how to use print function in python 
Python :: how to check if file exists pyuthon 
Python :: how to use with open 
Python :: get title attribute beautiful soup 
Python :: how to import flask restful using pip 
Python :: Substring in a django template? 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =