简体   繁体   English

在Python中,如何在排序列表中找到大于阈值的第一个值的索引?

[英]In Python, how do you find the index of the first value greater than a threshold in a sorted list?

In Python, how do you find the index of the first value greater than a threshold in a sorted list?在Python中,如何在排序列表中找到大于阈值的第一个值的索引?

I can think of several ways of doing this (linear search, hand-written dichotomy,..), but I'm looking for a clean an reasonably efficient way of doing it.我可以想到几种方法来做到这一点(线性搜索,手写二分法,..),但我正在寻找一种干净且合理有效的方法。 Since it's probably a pretty common problem, I'm sure experienced SOers can help!由于这可能是一个非常普遍的问题,我相信有经验的 SOers 可以提供帮助!

Thanks!谢谢!

Have a look at bisect .看看bisect

import bisect

l = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

bisect.bisect(l, 55) # returns 7

Compare it with linear search:将其与线性搜索进行比较:

timeit bisect.bisect(l, 55)
# 375ns


timeit next((i for i,n in enumerate(l) if n > 55), len(l))
# 2.24us


timeit next((l.index(n) for n in l if n > 55), len(l))
# 1.93us

You might get a better time than the enumerate/generator approach using itertools;您可能会比使用 itertools 的枚举/生成器方法获得更好的时间; I think itertools provides faster implementations of the underlying algorithms, for the performance mongers in all of us.我认为 itertools 为我们所有人的性能贩子提供了更快的底层算法实现。 But bisect may still be faster.但是 bisect 可能仍然更快。

from itertools import islice, dropwhile

threshold = 5
seq = [1,4,6,9,11]
first_val = islice(dropwhile(lambda x: x<=threshold, seq),0,1)
result = seq.index(first_val)

I wonder about the difference between the bisect approach shown here and the one listed for your question in the doc examples, as far as idiom/speed.我想知道这里显示的二等分方法与文档示例中为您的问题列出的方法之间的区别,就习惯用法/速度而言。 They show an approach for finding the value, but truncated to first line, it returns the index.他们展示了一种查找值的方法,但被截断到第一行,它返回索引。 I'd guess that since it's called "bisect_right" instead of "bisect," it probably only looks from one direction.我猜因为它被称为“bisect_right”而不是“bisect”,它可能只从一个方向看。 Given that your list is sorted and you want greater-than, this might be the greatest search economy.鉴于您的列表已排序并且您想要大于,这可能是最大的搜索经济。

from bisect import bisect_right

def find_gt(a, x):
    'Find leftmost value(switching this to index) greater than x'
    return bisect_right(a, x)

Interesting question.有趣的问题。

Related Index and Val of the last element greater than a threshold大于阈值的最后一个元素的相关索引和值

l = [1, 4, 9, 16, 25, 36, 49, 64, 100, 81, 100]
max((x,i) for i, x in enumerate(l) if x > 4)
(100, 10)

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

相关问题 Python二进制搜索类函数,用于查找排序列表中第一个大于特定值的数字 - Python binary search-like function to find first number in sorted list greater than a specific value Python:反向排序列表中第一个元素的索引小于阈值 - Python: index of fist element smaller than threshold in reverse sorted list 在python的列表中查找大于0的第一个和最后一个值 - Find first and last value greater than 0 in a list in python 第一个大于 x 的 Python 列表索引? - First Python list index greater than x? 查找所有列的第一个值小于阈值的索引 - Find index of first value less than threshold for all columns 如何在 python 中找到列表的排序位置/索引? - How do I find the sorted position/index of a list in python? 在 +ve 斜率中找到大于阈值的值的索引的最佳方法 - best way to find the index of value greater than threshold value in +ve slope 找到 x 中大于 0.999 的第一个值的索引 - find the index of the first value in x that is greater than 0.999 Python:沿着特定维度查找最大数组索引,该索引大于阈值 - Python: Find largest array index along a specific dimension which is greater than a threshold 在Python中有效地查找大于阈值的第一个样本(和MATLAB比较) - Finding first samples greater than a threshold value efficiently in Python (and MATLAB comparison)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM