Hopefully this will make sense...
I have a list of tuples of the following form:
list_of_tuples = [('a', 1), ('b', 3), ('b', 2), ('a', 3)]
The desired output is
sorted_list_of_tuples = [('a', 1), ('b', 2), ('b', 3), ('a', 3)]
The issue is I want the second entry to be increasing, and the first entry to be decreasing.
import operator as op
sorted_list_of_tuples = sorted(list_of_tuples, key=op.itemgetter(2, 0))
This, of course, sorts both fields to be increasing. I can't come up with a cute (i.e., couple of lines) way to do this. Does anyone have a way to accomplish this kind of sorting easily?
I seem to remember that you can reference elements of a list comprehension inside the brackets using _
, so maybe that's a place to start?
Maybe I wasn't clear: the integer in this case is more important. It's order should be increasing across the list. When there's a tie (ie.., the second entry is equal), I want 'b' to occur before 'a'.