python - How to sort in decreasing value first then increasing in second value -
this question has answer here:
let have :
student_tuples = [ ('john', 'a', 15), ('peter', 'b', 12), ('dave', 'c', 12)] how sort this:
student_tuples = [('john', 'a', 15), ('dave', 'c', 12), ('peter', 'b', 12)] what can think is:
from operator import itemgetter sorted(student_tuples, key=itemgetter(2,0), reverse=true) but output be:
student_tuples = [('john', 'a', 15), ('peter', 'b', 12), ('dave', 'c', 12)] and not want. how can using itemgetter or other easier way?
this it:
print sorted(student_tuples, key=lambda t: (-t[2], t[0])) # [('john', 'a', 15), ('dave', 'c', 12), ('peter', 'b', 12)]
Comments
Post a Comment