python中的指数退避实现

python中的指数退避实现,python,recursion,exponential,exponential-backoff,Python,Recursion,Exponential,Exponential Backoff,我有两个列表“开始”和“结束”。长度相同(各400万): 我的问题是,我想迭代两个列表,从同一点开始,但沿着“结束列表”逐步迭代,直到找到一个值,其中“开始[I]”和“结束[I+x]”之间的差值大于1000 我已经尽了最大的努力,在这里,我使用一个无休止的循环来迭代“结束列表”,直到与start的差值超过1000,然后从该点开始执行相同的操作 注意:省略旧内容 最终,我要寻找的输出是(以上图为例): 有人能帮我吗 更新 虽然前面对这个问题的回答确实有效: density=[] i_s = 0

我有两个列表“开始”和“结束”。长度相同(各400万):

我的问题是,我想迭代两个列表,从同一点开始,但沿着“结束列表”逐步迭代,直到找到一个值,其中“开始[I]”和“结束[I+x]”之间的差值大于1000

我已经尽了最大的努力,在这里,我使用一个无休止的循环来迭代“结束列表”,直到与start的差值超过1000,然后从该点开始执行相同的操作

注意:省略旧内容

最终,我要寻找的输出是(以上图为例):

有人能帮我吗

更新

虽然前面对这个问题的回答确实有效:

density=[]
i_s = 0
while i_s < len(start):
     i_e = i_s
     while i_e < len(end):
         if end[i_e] - start[i_s] > 1000:
             density.append(i_e - i_s + 1)
             i_s = i_e
             break
         i_e += 1
     i_s += 1


print sum(density)/float(len(density))
print max(density)
print min(density)

我对这种问题没有经验,也不确定我是否正确处理了它。。。有人能帮忙吗?

不确定我是否理解正确。。。这就是你要找的吗

MAX_DIFF = 1000
density = [0] * len(start)
for i in range(len(start)):
    for j in range(i, len(end)):
        density[i] += 1
        if end[i] - start[i] >= MAX_DIFF:
            break
print(density)

这是我发现
while
循环更直接的少数情况之一,因为
I_e
I_s
相互依赖。您可以使用两个
range
迭代器,并通过消耗后者的量来推进前者,但这似乎过于复杂

>>> start
[3000027, 3000162, 3000186, 3000187, 3005000, 3005020, 3007000, 3007186, 3009000]
>>> end
[3000162, 3000186, 3000187, 3005000, 3005020, 3005090, 3007186, 3009000, 3009500]
>>> i_s = 0
>>> while i_s < len(start):
...     i_e = i_s
...     while i_e < len(end):
...         if end[i_e] - start[i_s] > 1000:
...             print(i_e - i_s + 1)
...             i_s = i_e
...             break
...         i_e += 1
...     i_s += 1
...
4
3
1
>>开始
[3000027, 3000162, 3000186, 3000187, 3005000, 3005020, 3007000, 3007186, 3009000]
>>>结束
[3000162, 3000186, 3000187, 3005000, 3005020, 3005090, 3007186, 3009000, 3009500]
>>>i_s=0
>>>当i_s1000:
...             打印(i_e-i_s+1)
...             i_s=i_e
...             打破
...         i_e+=1
...     i_s+=1
...
4.
3.
1.

开始列表和结束列表是否都已排序(排序)?为什么在while循环之后所有内容都没有缩进?我想应该是这样。抱歉,代码现在应该是缩进的,是的,两个列表都是排序的。我只是不明白你想做什么。为什么末尾列表中的
4005090
与后面1个位置的开始列表中的
300500
相比不符合条件?您想要的输出是什么?我还是不明白。OP想在内存中存储
len(start)
列表吗?(s) 他说,名单上有400万个元素long@dabadaba嗯,我不能肯定,但这似乎是预期的产出?。。。但是,如果必要的话,它可以表示为一个生成器。如果你打算使用
while
,你可以将条件
end[i_e]-start[i_s]>=1000
移动到
while
上,这样你就不需要
中断
@dabadababa-hm,也许我想得太多了,但是,如果这个条件永远得不到呢?那么我猜它会耗尽列表吗?@dabadaba当你试图索引到列表中时,你会抛出一个错误。。。没有要使用的列表迭代器…但是您已经在检查
i\u e
,它不会超出范围
counter=1 ##### initialise counter with value of 1
def exponentially_increase_decrease(start, end, counter):
    distance=end-start
    if distance<=500:  ###500 is half the desired distance
        counter=exponentially_increase_decrease(start, end, counter*2)
    else:
        counter=-exponentially_increase_decrease(start, end, counter/2)
    print counter
    return counter
density=[]
i_s = 0
while i_s < len(start):
     i_e = i_s
     while i_e < len(end):
         if end[i_e] - start[i_s] > 1000:
             density.append(i_e - i_s + 1)
             i_s = i_e
             break
         counter=counter=exponentially_increase_decrease(i_s, i_e, counter)
         i_e += counter
     i_s += 1
counter=exponentially_increase_decrease(start, end, counter*2)
RuntimeError: maximum recursion depth exceeded
MAX_DIFF = 1000
density = [0] * len(start)
for i in range(len(start)):
    for j in range(i, len(end)):
        density[i] += 1
        if end[i] - start[i] >= MAX_DIFF:
            break
print(density)
>>> start
[3000027, 3000162, 3000186, 3000187, 3005000, 3005020, 3007000, 3007186, 3009000]
>>> end
[3000162, 3000186, 3000187, 3005000, 3005020, 3005090, 3007186, 3009000, 3009500]
>>> i_s = 0
>>> while i_s < len(start):
...     i_e = i_s
...     while i_e < len(end):
...         if end[i_e] - start[i_s] > 1000:
...             print(i_e - i_s + 1)
...             i_s = i_e
...             break
...         i_e += 1
...     i_s += 1
...
4
3
1