Python中的元组索引超出范围

时间:2016-10-20 01:15:00

标签: python indexing tuples

def max_logged_in(interval_lst,T):
    startArr, endArr = zip(*interval_lst)
    i = 0
    j = 0
    maxOverlap = 0
    currentOverlap = 0
    while (i<T and j<T):
        if (startArr[i] < endArr[j]):
            currentOverlap = currentOverlap + 1
            maxOverlap = max(maxOverlap, currentOverlap)
            i = i + 1
        else:
            currentOverlap = currentOverlap - 1
            j = j + 1

代码应该通过两个数组运行,并找到最大重叠在给定时间内给出一个列表,例如[(5,15), (18,25), (3,12), (4, 11), (1,15), (18,19)]T)。 运行此代码给我一个元组索引错误。我似乎无法弄清楚它为什么会给我一个索引错误。

1 个答案:

答案 0 :(得分:1)

如果T大于interval_lst的长度,则i和/或j可以达到该长度,但任何一个都没有元素该索引处startArrendArr(因为它们的长度与interval_lst相同)。