【问题标题】:Finding the index of an element in a sorted list efficiently有效地在排序列表中查找元素的索引
【发布时间】:2014-04-30 22:51:08
【问题描述】:

我有一个排序列表l(大约有 20,000 个元素),并且想在l 中找到超过给定值 t_min 的第一个元素。目前,我的代码如下。

def find_index(l):   
    first=next((t for t in l if t>t_min), None) 
    if first==None:
        return None
    else:
        return l.index(first)

为了对代码进行基准测试,我使用cProfile 运行测试循环,并通过将时间与控制循环进行比较来去除随机生成列表所需的时间:

import numpy
import cProfile

def test_loop(n):
    for _ in range(n):
        test_l=sorted(numpy.random.random_sample(20000))
        find_index(test_l, 0.5)

def control_loop(n):
    for _ in range(n):
        test_l=sorted(numpy.random.random_sample(20000))

# cProfile.run('test_loop(1000)') takes 10.810 seconds
# cProfile.run('control_loop(1000)') takes 9.650 seconds

find_index 的每个函数调用大约需要 1.16 毫秒。鉴于我们知道列表已排序,有没有办法改进代码以使其更高效?

【问题讨论】:

标签: python sorting python-2.7 optimization


【解决方案1】:

标准库bisect 模块对此很有用,而文档contain an example 正是这个用例。

def find_gt(a, x):
    'Find leftmost value greater than x'
    i = bisect_right(a, x)
    if i != len(a):
        return a[i]
    raise ValueError

【讨论】:

  • 感谢您的回答 - 我不知道 bisect。不过,我想我正在寻找find_gt 而不是这里的索引。
猜你喜欢
  • 1970-01-01
  • 2013-07-26
  • 2021-06-25
  • 2011-03-03
  • 2010-12-02
  • 2018-08-21
  • 2014-09-03
  • 2013-01-17
  • 2016-03-22
相关资源
最近更新 更多