简体   繁体   English

在找到第一个> =元素的索引时

[英]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 . 给定整数的有序列表A和整数q <= A[-1] ,我想找到最小的非负索引i ,使得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. 我可以考虑有效地执行此操作的方法 (例如,使用某种形式的二进制搜索),但是我想知道Python的标准库或numpy / scipy中是否有任何可用于实现此功能的内容。

(For example, something analogous to MATLAB's interp1 function.) (例如,类似于MATLAB的interp1函数。)

Python provides a module for performing binary search operations on a sorted list: bisect . Python提供了一个用于在有序列表bisect上执行二进制搜索操作的模块。 I believe the function you want is bisect_left . 我相信您想要的功能是bisect_left

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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