2

Let me tell you what I mean. Let's say I have a list that consists of 75, 250, 525, and 900. And let's say a random number, 95. How can I round 95 to 250?

So far, I have only made it, so it rounds to the closest number but not the next integer.

def closest(lst, K): 
      
    return lst[min(range(len(lst)), key = lambda i: abs(lst[i]-K))] 
      
lst = [75, 250, 525, 900] 
K = 95
print(closest(lst, K))
danronmoon
  • 3,814
  • 5
  • 34
  • 56
raid6n
  • 55
  • 1
  • 7
  • 2
    Yes, it will be least to greatest. – raid6n Mar 01 '21 at 00:26
  • Simple: iterate the list and return the first number from the list that is equal or greater than the input. No idea why you think you need such a complicated computation here. – GhostCat Mar 01 '21 at 00:26
  • 4
    Does this answer your question? [Python binary search-like function to find first number in sorted list greater than a specific value](https://stackoverflow.com/questions/3556496/python-binary-search-like-function-to-find-first-number-in-sorted-list-greater-t) – user202729 Mar 01 '21 at 00:27
  • And if the list isn't sorted, then either sort it initially, or while iterating, remember the currently picked result number and see whether the other entries are smaller than that but bigger than the input. – GhostCat Mar 01 '21 at 00:29
  • 2
    Bisect catalog: [less/less or equal (unclear)](https://stackoverflow.com/q/36143149), [less or equal](https://stackoverflow.com/q/2591159), [index](https://stackoverflow.com/q/23390917), [C++ equivalent](https://stackoverflow.com/q/37873954), [greater (this question)](https://stackoverflow.com/q/66415225), [greater (unsorted!)](https://stackoverflow.com/questions/2236906) – user202729 Mar 01 '21 at 00:41

3 Answers3

2

If your list is always sorted, you can use bisect:

import bisect 
lst = [75, 250, 525, 900] 
k=95

>>> lst[bisect.bisect_right(lst, k)]
250

You will need to decide what you want to return when k is larger than all numbers in lst As currently written, if k>=900 that would be an index error. Easily fixed with try ... except but you need to specify what the 'right' answer is in that case.

Bisect would be the best choice for large lists since it is written in C and super fast.

Alternatively, you can use the next built-in function:

>>> next((e for e in lst if e>k), default_if_not_found)
250

Same comment: you would need to define a default value if e>k cannot be found.

dawg
  • 98,345
  • 23
  • 131
  • 206
  • bisecting is likely always best with a list of any meaningful size! ..but note this should try/except or include a maximum value as it'll `raise IndexError` for values outside of it – ti7 Mar 01 '21 at 00:34
  • 1
    @raid6n Having chosen this as the best answer, please make sure that for your own needs, you know for sure that K is always between `list[0]` and `list[-1]` and that the input list is never empty. This code needs a check for those conditions if they are possible, as the current version will throw an exception if `K` isn't so well behaved. – CryptoFool Mar 01 '21 at 00:42
0

I'm old school. I'd prefer something with a few more lines that anyone who sees the code can understand:

def closest(lst, n):
    for i in lst:
        if i > K:
            return i
    return None

K = 95
lst = [75, 250, 525, 900]

print(closest(lst, K))
Rapt0r
  • 79
  • 3
0

As long as the list is guaranteed to be sorted this should work, returning None if k is greater than all elements in l.

def closest(l, k):
    for i in l:
        if k < i:
            return i
    return None
      
res = closest([75, 250, 525, 900] , 95)
greenBox
  • 552
  • 4
  • 5