kjo
kjo

Reputation: 35301

On finding the index of the first >= element

Given an ordered list A of integers, and an integer q <= A[-1], I want to find the smallest non-negative index i such that A[i] >= q.

I can think of ways to do this efficiently (e.g. with some form of binary search), but I'd like to know if there's anything in either Python's standard library or in numpy/scipy that I can use to implement this.

(For example, something analogous to MATLAB's interp1 function.)

Upvotes: 2

Views: 50

Answers (1)

augurar
augurar

Reputation: 13016

Python provides a module for performing binary search operations on a sorted list: bisect. I believe the function you want is bisect_left.

Upvotes: 5

Related Questions