python - How to sort similar values in a sorted list (based on second value) of tuples based on another value(third value) in the tuple in descending order -
i have list of tuples of format
[("d",21,5),(e,21,4),("a",20,1),("b",20,3),("c",20,2),...]
where first values (a,b,c etc) unique , other 2 values in tuple might repeat.(like 20)
i want sort list based on 2nd element(here 20,21) in tuple in ascending order, like
[a,b,c,d,e]
then want values sorted based on same numbers (a,b,c) sorted based on 20 , (d,e) based on 21 sorted based on 3rd value(here 1,2,3,4,5) of tuple in descending order, like
[b,c,a,d,e]
if original list looks this:
l = [("d",21,5),(e,21,4),("a",20,1),("b",20,3),("c",20,2),...]
then, can sort way want, defining 2-tuple key in sort function:
l.sort(key=lambda t: (t[1],-t[2]))
this ensures list sorted second element in tuple, while ties broken negated value of third element in tuple (i.e. in descending order third element)