The use of python sort function and lambda to achieve sorting by keyword

people.sort (key = lambda x: (-x [0], x [1])) # -x [0] means descending order, x [1] means ascending order

Input: [[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]

Output: [[5, 0], [7, 0], [5, 2], [6, 1], [4, 4], [7, 1]]

Published 141 original articles · Like 54 · Visits 150,000+

Guess you like

Origin blog.csdn.net/iamjingong/article/details/105655541
Recommended