1

First, I have noticed that there are many questions posted on sorting lists of tuples, however after looking over a few posts I did not see any of the questions with this particular format for the list. Apologies in advance then for a potential repeat question, however I think this should be simple.

Let the list of tuples be this:

my_list = [(('G', 'J', 'I'), 1.0), (('E', 'H', 'F'), 1.0), (('F', 'H', 'G'), 0.8889), (('I', 'K', 'J'), 0.8889), (('H', 'I', 'G'), 0.8889), (('H', 'J', 'I'), 0.875)]

Note that each tuple in the list consists of 1: another tuple of length 3 with 3 letters, and 2: a floating point number. My sorting objective is simple then: 1st sort the list of tuples by the floating point number, 2nd break any ties in the floating point number by then sorting by the first letter in the length-3 tuple, 3rd break any ties in that letter by sorting by the 2nd letter in the length-3 tuple. Desired output would then be:

sorted_list = [(('E', 'H', 'F'), 1.0), (('G', 'J', 'I'), 1.0), (('F', 'H', 'G'), 0.8889),  (('H', 'I', 'G'), 0.8889), (('I', 'K', 'J'), 0.8889), (('H', 'J', 'I'), 0.875)]

in this particular example, sorting on the 2nd letter to break ties on the first letter did not appear, although it does in my larger dataset.

Thanks!

2
  • 1
    So, you want to sort in descending order by the float, and in ascending order by the letters in the tuple? Try key=lambda t: (-t[1], t[0]) Commented Jan 23, 2017 at 23:09
  • Possible duplicate of Python 3.3 Sorting a tuple list with multiple keys Commented Jan 23, 2017 at 23:17

1 Answer 1

5

Here's one way to do it: the first sort is done in reverse on the float, while the ties are broken by sorting on the inner tuple:

srt_lst = sorted(my_list, key=lambda (x, y): (-y, x)) #python 2
print(srt_lst)
# [(('E', 'H', 'F'), 1.0), (('G', 'J', 'I'), 1.0), (('F', 'H', 'G'), 0.8889), (('H', 'I', 'G'), 0.8889), (('I', 'K', 'J'), 0.8889), (('H', 'J', 'I'), 0.875)]

In Python 3, you'll have to index the lambda's single parameter to access the items and use them for sorting:

srt_lst = sorted(my_list, key=lambda tup: (-tup[1], tup[0]))
Sign up to request clarification or add additional context in comments.

4 Comments

Note: tuple unpacking in lambda does not work in Python 3; otherwise fine.
@tobias_k Thanks for noting. Their Python 3 tag was missing
thanks - using python 2, and can confirm that the first solution sorted properly for me.
FWIW, I'd suggest only using the python3 variant (which works just fine on python2.x). Just because the python2.x variant works today, if you use decide to port your code to python3 tomorrow, then this is just another thing you'll have to fix... (and like it or not, python3.x is the future of python).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.