How to find second largest number in a list?

前端 未结 14 1977
遇见更好的自我
遇见更好的自我 2020-12-06 15:38

So I have to find the second largest number from list. I am doing it through simple loops.

My approach is to divide a list into two parts and then find the largest n

14条回答
  •  隐瞒了意图╮
    2020-12-06 16:01

    alist=[10, 0,3,10,90,5,-2,4,18,45,707, 100,1,-266,706, 1]
    print(max(alist))
    second_largest=alist[0]
    for i in range(len(alist)):
        if (alist[i]>second_largest and alist[i]!=max(alist)):
            second_largest=alist[i]
    print(second_largest)
    

提交回复
热议问题