nearest_unsorted

emlib.misc.nearest_unsorted(x, seq)[source]

Find nearest item in an unsorted sequence

Parameters:
  • x (Union[float, Rational]) – a number

  • seq (list[Union[float, Rational]]) – a seq. of numbers (assumes it is not sorted)

Return type:

Union[float, Rational]

Returns:

the item in seq. which is nearest to x

NB: for sorted seq. use nearest_index

Example

>>> assert nearest_unsorted(3.6, (1,2,3,4,5)) == 4
>>> assert nearest_unsorted(4, (2,3,4)) == 4
>>> assert nearest_unsorted(200, (3,5,20)) == 20