How to find second largest number in a list?

前端 未结 14 1963
遇见更好的自我
遇见更好的自我 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:06

     list1=[1,10,2,3,5,7,1,-32,90,99,99]
     max=0
     secmax=0
     for i in list1:
        if i>max:
           max=i
     for i in list1:
        if i>secmax and max!=i:
          secmax=i
     print(secmax)
    

提交回复
热议问题