简体   繁体   English

有效地查找数量最小的索引,该索引大于大型排序列表中的某个值

[英]Efficiently find index of smallest number larger than some value in a large sorted list

If I have a long list of sorted numbers, and I want to find the index of the smallest element larger than some value, is there a way to implement it more efficiently than using binary search on the entire list? 如果我有一个很长的排序数字列表,并且我想找到大于某个值的最小元素的索引,是否有比在整个列表上使用二进制搜索更有效地实现它的方法?

For example: 例如:

import random
c = 0
x = [0 for x in range(50000)]

for n in range(50000):
    c += random.randint(1,100)
    x[n] = c

What would be the most efficient way of finding the location of the largest element in x smaller than some number, z 在x中找到小于某个数字z的最大元素的位置的最有效方法是什么

I know that you can already do: 我知道您已经可以:

import bisect
idx = bisect.bisect(x, z)

But assuming that this would be performed many times, would there be an even more efficient way than binary search? 但是,假设此操作可以执行多次,那么还有比二进制搜索更有效的方法吗? Since the range of the list is large, creating a dict of all possible integers uses too much memory. 由于列表的范围很大,因此创建所有可能整数的字典将占用过多内存。 Would it be possible to create a smaller list of say every 5000 numbers and use that to speed up the lookup to a specific portion of the large list? 是否可以创建一个较小的列表(例如每5000个数字),并使用该列表来加快对较大列表的特定部分的查找?

Can you try if this can be a solution? 您能尝试一下是否可以解决? It takes long to generate the list, but seems fast to report the result. 生成列表需要很长时间,但是报告结果似乎很快。

Given the list: 给定列表:

import random
limit = 50 # to set the number of elements
c = 0
x = [0 for x in range(limit)]

for n in range(limit):
    c += random.randint(1,100)
    x[n] = c
print(x)

Since it is a sorted list, you could retrieve the value using a for loop: 由于它是一个有序列表,因此可以使用for循环来检索值:

z = 1600 # reference for lookup

res = ()
for i, n in enumerate(x):
  if n > z:
    res = (i, n)
    break

print (res) # this is the index and value of the elements that match the condition to break
print(x[res[0]-1]) # this is the element just before

暂无
暂无

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

相关问题 在排序列表中查找大于给定数字的最小数字 - Find the smallest number that is greater than a given number in a sorted list 如何查找值大于numpy中某个阈值的数组的索引? - How to find the index of an array where the value is larger than some threshold in numpy? 给定一个排序列表,找到数字的索引 - given a sorted list, find the index of number C 函数从按递增顺序排序的数组中返回大于给定数字的最小数字的索引 - C function that returns index of smallest number greater than a given number from an array sorted in increased order 如何获得排序列表中最大和最小的数字? - How to get largest and smallest number in a sorted list? 在Python中,如何在排序列表中找到大于阈值的第一个值的索引? - In Python, how do you find the index of the first value greater than a threshold in a sorted list? 在列表中找到最小值 - Find smallest value in list Python二进制搜索类函数,用于查找排序列表中第一个大于特定值的数字 - Python binary search-like function to find first number in sorted list greater than a specific value 在包含其他数据类型的列表中查找最小 integer 值的索引 - Find index of smallest integer value in list containing other datatypes 如何获取排序数组的索引,其中前n个值的摘要大于某个值 - How to get the index of an sorted array where the summary of first n values larger than a certain value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM