user8643832
user8643832

Reputation:

How to find the list with the the first value is the largest in python

Here i have a list of lists:

m = [[[0.5898237279659958, 620, 200]],[[0.58557319664958118, 720, 200]],[[0.5959494936867108, 820, 200]], [[0.59444930616327041, 920, 200]]]

I would like to find out the list which first value is the largest, for example, in m, i need to find out this [[0.5959494936867108, 820, 200]]

Upvotes: 0

Views: 53

Answers (3)

AJ123
AJ123

Reputation: 194

first of all, you don't need the double square brackets; this should work:

 m = [[0.5898237279659958, 620, 200],[0.58557319664958118, 720, 200], [0.5959494936867108, 820, 200], [0.59444930616327041, 920, 200]]
        nums = []
        for l in m:
            nums.append(l[0]) #Puts the first value of each list into a list, 
                              #0 can be changed to position in the list you want
        nums.sort() #Puts the largest value last
        cornum = nums[-1] #Gets the last number
        for l in m:
            if cornum in l: #checks if the number is in each of the lists in m
                print(l)

Upvotes: 0

wim
wim

Reputation: 362557

Lists are sorted lexicographically, so your answer is simply:

>>> max(m)
[[0.5959494936867108, 820, 200]]

Upvotes: 3

Ajax1234
Ajax1234

Reputation: 71451

You can try this:

m = [[[0.5898237279659958, 620, 200]],[[0.58557319664958118, 720, 200]],[[0.5959494936867108, 820, 200]], [[0.59444930616327041, 920, 200]]]
final_m = max(m, key=lambda x:x[0])

Output:

[[0.5959494936867108, 820, 200]]

Upvotes: 1

Related Questions