Return tuple with smallest y value from list of tuples

本秂侑毒 提交于 2019-12-04 03:20:29

Include the x value in a tuple returned from the key; this second element in the key will be then used when there is a tie for the y value. To inverse the comparison (from smallest to largest), just negate that value:

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

After all, -4 is smaller than -2.

Demo:

>>> x = [(2, 3), (4, 3), (6, 9)]
>>> min(x, key=lambda t: (t[1], -t[0]))
(4, 3)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!