Python - Find second smallest number

前端 未结 16 1158
长发绾君心
长发绾君心 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
    
    0 讨论(0)
  • 2020-11-28 11:21

    As per the Python in-built function sorted

    sorted(my_list)[0]
    

    gives back the smallest number, and sorted(my_list)[1] does accordingly for the second smallest, and so on and so forth.

    0 讨论(0)
  • 2020-11-28 11:22

    The function can indeed be modified to find the second smallest:

    def second_smallest(numbers):
        m1, m2 = float('inf'), float('inf')
        for x in numbers:
            if x <= m1:
                m1, m2 = x, m1
            elif x < m2:
                m2 = x
        return m2
    

    The old version relied on a Python 2 implementation detail that None is always sorted before anything else (so it tests as 'smaller'); I replaced that with using float('inf') as the sentinel, as infinity always tests as larger than any other number. Ideally the original function should have used float('-inf') instead of None there, to not be tied to an implementation detail other Python implementations may not share.

    Demo:

    >>> def second_smallest(numbers):
    ...     m1, m2 = float('inf'), float('inf')
    ...     for x in numbers:
    ...         if x <= m1:
    ...             m1, m2 = x, m1
    ...         elif x < m2:
    ...             m2 = x
    ...     return m2
    ... 
    >>> print second_smallest([1, 2, 3, 4])
    2
    

    Outside of the function you found, it's almost just as efficient to use the heapq.nsmallest() function to return the two smallest values from an iterable, and from those two pick the second (or last) value:

    from heapq import nsmallest
    
    def second_smallest(numbers):
        return nsmallest(2, numbers)[-1]
    

    Like the above implementation, this is a O(N) solution; keeping the heap variant each step takes logK time, but K is a constant here (2)! Whatever you do, do not use sorting; that takes O(NlogN) time.

    0 讨论(0)
  • 2020-11-28 11:22
     mi= min(input_list)
        second_min = float('inf')
        for i in input_list:
            if i != mi:
                if i<second_min:
                    second_min=i
        if second_min == float('inf'):
            print('not present')
        else:
            print(second_min)
                 
    
    ##input_list = [6,6,6,6,6]
    #input_list = [3, 1, 4, 4, 5, 5, 5, 0, 2, 2]
    #input_list = [7, 2, 0, 9, -1, 8]
    # Even if there is same number in the list then Python will not get confused.
    
    0 讨论(0)
  • 2020-11-28 11:28

    You can use in built function 'sorted'

    def second_smallest(numbers):
    
    count = 0
    l = []
    for i in numbers:
        if(i not in l):
            l.append(i)
            count+=1
        if(count==2):
            break
    
    return max(l)
    
    0 讨论(0)
  • 2020-11-28 11:28

    There is a easy way to do . First sort the list and get the second item from the list.

    def solution(a_list):
    
        a_list.sort()
        print a_list[1]
    
    solution([1, 2, -8, -2, -10])
    
    0 讨论(0)
提交回复
热议问题