简体   繁体   中英

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 (eg 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.)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM