nearest_index

emlib.misc.nearest_index(item, seq)[source]

Return the index of the nearest element in seq to item

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

  • seq (Sequence[Union[Rational, float]]) – a sorted sequence of numbers

Return type:

int

Returns:

the index of the nearest item

NB: Assumes that seq is sorted

Example:

>>> seq = [0, 3, 4, 8]
>>> nearest_index(3.1, seq)
1
>>> nearest_index(6.5, seq)
3