简体   繁体   中英

Tuple index out of range in Python

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

The code is supposed to run through the two arrays and find the max overlap give a list such as [(5,15), (18,25), (3,12), (4, 11), (1,15), (18,19)] in the given time ( T ). Running this code is giving me a tuple index error. I can't seem to figure out why it is giving me an index error.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM