nearest_index¶
- emlib.misc.nearest_index(item, seq)[source]¶
Return the index of the nearest element in seq to item
- Parameters:
item (
TypeVar(num_t,int,float,Rational)) – a numberseq (
Sequence[TypeVar(num_t,int,float,Rational)]) – a sorted sequence of numbers
- Return type:
int- Returns:
the index of the nearest item
Note
Assumes that seq is sorted
Example
>>> seq = [0, 3, 4, 8] >>> nearest_index(3.1, seq) 1 >>> nearest_index(6.5, seq) 3
See also