Python - Find second smallest number

前端 未结 16 1225
长发绾君心
长发绾君心 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:20

    Or just use heapq:

    import heapq
    def second_largest(numbers):
        return heapq.nsmallest(2, numbers)[-1]
    
    second_largest([1, 2, 3, 4])
    # Output: 2
    

提交回复
热议问题