Find The Closest Number To Numbers Given In A List ~ Python

How would you find the closest number in comparison to numbers given in a list?

This is what I have tried so far, but it has been unsuccessful:

setted_list = [2, 9, 6, 20, 15]
value_chosen = 17

while True:
    final_value = setted_list[0]
    if setted_list[1] - value_chosen < setted_list[0] - value_chosen:
        final_value = setted_list[1]
    if setted_list[2] - value_chosen < setted_list[1] - value_chosen:
        final_value = setted_list[2]
    if setted_list[3] - value_chosen < setted_list[2] - value_chosen:
        final_value = setted_list[3]
    if setted_list[4] - value_chosen < setted_list[3] - value_chosen:
        final_value = setted_list[4]
print(final_value)

My output is always what is inside the value of setted_list[2]. Where have I gone wrong in my algorithm?

Upvotes: 5

Views: 13858

Answers (5)

Jos&#233; Garcia
Jos&#233; Garcia

Reputation: 136

Here I use abs() to determine the closest value by subtracting it from the original value

slist = [2, 9, 6, 20, 15]
value = 17

def closest_value(value, iterable):
    storage = []

    for i in iterable:
        storage.append((abs(value - i),i))

    result = min(storage)
    return print(result[1])

closest_value(value, slist)
15

Upvotes: 0

Gingerbread
Gingerbread

Reputation: 2122

If you are too new to understand lambda functions yet,

   minimum = float("inf")
   setted_list = [2, 9, 6, 20, 15]
   value_chosen = 17

   for val in setted_list:
       if abs(val - value_chosen) < minimum:
           final_value = val
           minimum = abs(val - value_chosen)

            
   print final_value

Upvotes: 1

Ryan Burgert
Ryan Burgert

Reputation: 553

Here's a nice, clean simple one-liner: check this out you'll hopefully learn something new 😊 (@ the OP)

print min(setted_list,
          key = lambda x: abs(x-value_chosen))

The min() here does not returned the minimalst value from setted_list. The question What is the minimalst goes to the key= argument. The lambda function create a list() of the difference between each element in setted_list and value_chosen. The minimalst value is not the return value of min() but the index of the minimalst value is used to return the corresponding element in setted_list.

Upvotes: 15

Dadep
Dadep

Reputation: 2788

The loop while True: never break... you need to find the end point.

maybe you wanted to do something like :

>>> l=max(setted_list)
>>> for i in setted_list:
...     if abs(i-value_chosen)<l:
...             l=abs(i-value_chosen)
...             final_value=i
... 
>>> final_value
15

you can also do something like :

>>> setted_list = [2,9,6,20,15]
>>> value_chosen = 17
>>> min(setted_list, key=lambda x:abs(x-value_chosen))
15

Upvotes: 5

dvitsios
dvitsios

Reputation: 458

l = [2,9,6,20,15]
val = 17

min_d = max(l) - val
closest_num = -1
for i in l:
    if abs(i - val) < min_d:
        min_d = abs(i-val)
        closest_num = i

print(closest_num)  
#>>> 15

Upvotes: 0

Related Questions