I am finding the second largest number from the array list, but I get runtime-error when the input arr is 6,6,6,6,6,6,6,6,6,5

时间:2019-05-31 11:35:34

标签: python visual-studio-code runtime-error runtime

when i compiled this program in VSCode, I got IndexError. Is there any other solution?

#here is my sample code
if __name__ == '__main__':
    n = int(input())
    arr = list(map(int, input().split()))
    x = len(arr)
    arr.sort()
    for i in range(0, x-1):
        #removing redundant values
        if arr[i] == max(arr):
            arr.remove(arr[i])

    arr.remove(max(arr))
    print(max(arr))

2 个答案:

答案 0 :(得分:0)

split using split(',') when input is 6,6,6,6,6,6,6,6,6,5

arr = list(map(int, input().split(',')))

答案 1 :(得分:0)

由于您没有在第一次迭代后更新长度,您最终会认为删除了所有数组,因此认为数组比您大。导致出现IndexError。

如果您要复制数组,则始终可以对其进行迭代,从而允许您同时编辑原始列表。