Python - Find second smallest number

前端 未结 16 1156
长发绾君心
长发绾君心 2020-11-28 10:35

I found this code on this site to find the second largest number:

def second_largest(numbers):
    m1, m2 = None, None
    for x in numbers:
        if x >         


        
16条回答
  •  攒了一身酷
    2020-11-28 11:15

    To find second smallest in the list, use can use following approach which will work if two or more elements are repeated.

    def second_smallest(numbers):
         s = sorted(set(numbers))
         return s[1]
    

提交回复
热议问题