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
Post a Comment