Tuple pairs, finding minimum using python

后端 未结 3 2008
再見小時候
再見小時候 2020-12-23 09:06

I want to find the minimum of a list of tuples sorting by a given column. I have some data arranged as a list of 2-tuples for example.

data = [ (1, 7.57), (         


        
3条回答
  •  佛祖请我去吃肉
    2020-12-23 09:37

    Even though Lev's answer is correct, I wanted to add the sort Method as well, in case someone is interested in the first n minimas. One thing to consider is that the min operation's runtime is O(N) where the sort's is O(N Log N)

    data = [ (1, 7.57), (2, 2.1), (3, 1.2), (4, 2.1), (5, 0.01), (6, 0.5), (7, 0.2), (8, 0.6)]
    data.sort(key=lambda x:x[1])
    print data
    
    >>> [(5, 0.01), (7, 0.2), (6, 0.5), (8, 0.6), (3, 1.2), (2, 2.1), (4, 2.1), (1, 7.57)]
    

    https://www.ics.uci.edu/~pattis/ICS-33/lectures/complexitypython.txt

提交回复
热议问题