list1=[('Oliver', 75, 180), ('Jack', 90, 190), ('Harry', 75, 175), ('Jacob', 60, 175), ('Charlie', 80, 180), ('Thomas', 70, 180)]
sorted(list1 , key = itemgetter(2 , 1) , reverse=True)
i wanted if persons height the same the person with less weight printed above my code output:
[('Jack', 90, 190), ('Charlie', 80, 180), ('Oliver', 75, 180), ('Thomas', 70, 180), ('Harry', 75, 175), ('Jacob', 60, 175)]
my desire output:
[('Jack', 90, 190), ('Thomas', 70, 180),('Oliver', 75, 180),('Charlie', 80, 180), ('Jacob', 60, 175), ('Harry', 75, 175)]
i use python3
You can try
sorted(list1 , key = lambda x: (x[2], -x[1]) , reverse=True)
Output
[('Jack', 90, 190),
('Thomas', 70, 180),
('Oliver', 75, 180),
('Charlie', 80, 180),
('Jacob', 60, 175),
('Harry', 75, 175)]
This will sort the list by the highest height and the lowest weight in case of the same height.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.