python - Return tuple with smallest y value from list of tuples -


i trying return tuple smallest second index value (y value) list of tuples. if there 2 tuples lowest y value, select tuple largest x value (i.e first index).

for example, suppose have tuple:

x = [(2, 3), (4, 3), (6, 9)] 

the value returned should (4, 3). (2, 3) candidate, x[0][1] 3 (same x[1][1]), however, x[0][0] smaller x[1][0].

so far have tried:

start_point = min(x, key = lambda t: t[1]) 

however, checks second index, , not compare 2 tuples first index if second index's equivalent.

include x value in tuple returned key; second element in key used when there tie y value. inverse comparison (from smallest largest), negate value:

min(x, key=lambda t: (t[1], -t[0])) 

after all, -4 smaller -2.

demo:

>>> x = [(2, 3), (4, 3), (6, 9)] >>> min(x, key=lambda t: (t[1], -t[0])) (4, 3) 

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -