I have a question from algorithm point of view.
I have a list of numbers (floats)
1.22,3.2, 4.9,12.3.....and so on
And I want to find the smallest number greater than (lets say) 4..
So the answer is 4.9
But besides the obvious solution.. (iterating thru list and keeping a track of smallest number greater than k) what is the “pythonic way” to do this.
Thanks
Binary search would be a standard way to deal with this, but only if the list is sorted, as previous answer pointed out.
See Python binary search-like function to find first number in sorted list greater than a specific value
and In Python, how do you find the index of the first value greater than a threshold in a sorted list?
for discussion of a module that does this for you: http://docs.python.org/library/bisect.html