How can I get the minimum and the maximum element of a list in python

前端 未结 8 1657
执笔经年
执笔经年 2021-01-16 18:06

If a have a list like:

l = [1,2,3,4,5]

and I want to have at the end

min = 1   
max = 5

WITHOUT

8条回答
  •  长发绾君心
    2021-01-16 18:26

    The fastest approach I can think of would be to sort the original list and then pick the first and last elements. This avoids looping multiple times, but it does destroy the original structure of your list. This can be solved by simply copying the list and sorting only the copied list. I was curious if this was slower than just using max() and min() with this quick example script:

    import time
    
    l = [1,2,4,5,3]
    
    print "Run 1"
    t1 = time.time()
    print "Min =", min(l)
    print "Max =", max(l)
    print "time =", time.time() - t1
    print ""
    print "l =", l
    print ""
    
    
    l = [1,2,4,5,3]
    l1 = list(l)
    
    print "Run 2"
    t1 = time.time()
    l1.sort()
    print "Min =", l1[0]
    print "Max =", l1[-1]
    print "time =", time.time() - t1
    print ""
    print "l =", l
    print "l1 =", l1
    print ""
    
    
    l = [1,2,4,5,3]
    
    print "Run 3"
    minimum = float('inf')
    maximum = float('-inf')
    for item in l:
        if item < minimum:
            minimum = item
        if item > maximum:
            maximum = item
    print "Min =", minimum
    print "Max =", maximum
    print "time =", time.time() - t1
    print ""
    print "l =", l
    

    Surprisingly, the second approach is faster by about 10ms on my computer. Not sure how effective this would be with very large list, but this approach is faster for at least the example list you provided.

    I added @Martijn Pieters's simple loop algorithm to my timing script. (As timing would be the only important parameter worth exploring in this question.) My results are:

    Run 1: 0.0199999809265s
    Run 2: 0.00999999046326s
    Run 3: 0.0299999713898s
    

    Edit: Inclusion of timeit module for timing.

    import timeit
    from random import shuffle
    
    l = range(10000)
    shuffle(l)
    
    def Run_1():
        #print "Min =", min(l)
        #print "Max =", max(l)
        return min(l), max(l)
    
    def Run_2():
        l1 = list(l)
        l1.sort()
        #print "Min =", l1[0]
        #print "Max =", l1[-1]
        return l1[0], l1[-1]
    
    
    def Run_3():
        minimum = float('inf')
        maximum = float('-inf')
        for item in l:
            if item < minimum:
                minimum = item
            if item > maximum:
                maximum = item
        #print "Min =", minimum
        #print "Max =", maximum
        return minimum, maximum
    
    
    if __name__ == '__main__':
        num_runs = 10000
        print "Run 1"
        run1 = timeit.Timer(Run_1)
        time_run1 = run1.repeat(3, num_runs)
        print ""
        print "Run 2"
        run2 = timeit.Timer(Run_2)
        time_run2 = run2.repeat(3,num_runs)
        print ""
        print "Run 3"
        run3 = timeit.Timer(Run_3)
        time_run3 = run3.repeat(3,num_runs)
        print ""
    
        print "Run 1"
        for each_time in time_run1:
            print "time =", each_time
        print ""
        print "Run 2"
        for each_time in time_run2:
            print "time =", each_time
        print ""
        print "Run 3"
        for each_time in time_run3:
            print "time =", each_time
        print ""
    

    My results are:

    Run 1
    time = 3.42100585452
    time = 3.39309908229
    time = 3.47903182233
    
    Run 2
    time = 26.5261287922
    time = 26.2023346397
    time = 26.7324208568
    
    Run 3
    time = 3.29800945144
    time = 3.25067545773
    time = 3.29783778232
    

    sort algorithm is very slow for large arrays.

提交回复
热议问题