-1

I'm trying to find the closest number in a list to a certain variable, I have managed the first step,

def closest(lst, K):
    
    return lst[min(range(len(lst)), key = lambda i: abs(lst[i]-K))]

lst = [1.25, 1.35, 2.12, 2.35, 2.45, 3.44, 4.01, 4.56, 4.85, 5.02, 5.56, 6.52,7.56]

K = 4.36

print(closest(lst,K)) #Output: 4.56

the second step however has been a challenge. i wanted now to be able not only to find the closest number, to a variable but also to make it more specific such that the closest number should also be less than the variable. The output for that function above is 4.56, I would however like to get an output of 4.01.

Any advice on how I can alter my code to come but that output?

Guy
  • 46,488
  • 10
  • 44
  • 88
  • Make the distance `float(Inf)` if it's in the wrong direction? – Jiří Baum Aug 15 '21 at 11:12
  • See https://stackoverflow.com/questions/8584902/get-the-closest-number-out-of-an-array/8584940#8584940 for a good start. – paxdiablo Aug 15 '21 at 11:20
  • Hi @SamuelByegon if any answer has solved your question please consider [accepting it](https://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – TYeung Sep 01 '21 at 15:50

4 Answers4

0

Does this achieve what you want?

def closest(lst, K):
    
    return lst[min(range(len(lst)), key = lambda i: K - lst[i] if K >= lst[i] else float('inf'))]

lst = [1.25, 1.35, 2.12, 2.35, 2.45, 3.44, 4.01, 4.56, 4.85, 5.02, 5.56, 6.52,7.56]

K = 4.36

print(closest(lst,K)) #Output: 4.01
TYeung
  • 2,579
  • 2
  • 15
  • 30
  • Thank you for your contribution. what does the last part do "else float ('inf')"? A further explanation will be greatly appreciated. – Samuel Byegon Aug 16 '21 at 16:14
  • @SamuelByegon `float("inf")` set a variable with infinitely large value, therefore making it not a candidate for closest element if `K - lst[i] < 0` – TYeung Aug 16 '21 at 16:17
0

You can just filter out all elements in lst that are greater than K. Try this:

def closest(lst, K):
    less_than_k = [x for x in lst if x < K]
    return less_than_k[min(range(len(less_than_k)), key=lambda i: abs(less_than_k[i] - K))]


lst = [1.25, 1.35, 2.12, 2.35, 2.45, 3.44, 4.01, 4.56, 4.85, 5.02, 5.56, 6.52, 7.56]
K = 4.36

print(closest(lst, K))  # Output: 4.01

Gabio
  • 9,126
  • 3
  • 12
  • 32
0

see below

import sys

lst = [1.25, 1.35, 2.12, 2.35, 2.45, 3.44, 4.01, 4.56, 4.85, 5.02, 5.56, 6.52, 7.56]

K = 4.36
delta = sys.float_info.max
close_number = sys.float_info.max
for num in lst:
    if num < K:
        if K - num < delta:
            delta = K - num
            close_number = num

print(close_number)

output

4.01
balderman
  • 22,927
  • 7
  • 34
  • 52
0

Although this is a little different, you can try this:

def closest(lst, K):
    j={k:abs(k-K) for k in lst if k<K}
    return min(j,key=j.get)
lst = [1.25, 1.35, 2.12, 2.35, 2.45, 3.44, 4.01, 4.56, 4.85, 5.02, 5.56, 6.52,7.56]

K = 4.36

print(closest(lst,K)) 

Here is how the logic works:

If you notice, when you want to get a particular number which is closest to some number, you will notice that the difference is the least among other numbers.

So using this logic, it is creating a dictionary where the key is the number and the value is the difference of the number and the other number which is provided as a parameter. You will also notice that I have used abs. That is used to find the absolute value of the number. If I don't use that, then the numbers smaller than the one provided will have difference that are negative numbers. So, they of course will be less than positive numbers.

And the statement return min(k,key=j.get) tells that min function should get the minimum value from the dictionary j based on the values of j