6

Is there a quick and easy way to sort a list of tuples containing two items? (a short list of the tuples I am trying to sort:

[('this', 4), ('in', 4), ('dedicated', 4), ('who', 3), ('us', 3), ('they', 3), ('so', 3), ('shall', 3), ('people', 3), ('is', 3), ('great', 3), ('dead', 3), ('are', 3), ('It', 3), ('which', 2), ('what', 2)]

I am trying to sort them first by frequency (largest first), so the number, and then by alphabetical order.

This is what I have so far:

word_list.sort(key=itemgetter(1,0), reverse = True)

This sorts the list by frequency in descending order.

1
  • So what is the problem actually? Your input list already seems to be sorted. Commented Mar 27, 2015 at 2:09

2 Answers 2

3

I think I understand what you want to do. Have order of frequencies different than words. For this you need to sort twice:

from operator import itemgetter

word_list = [('this', 4), ('in', 4), ('dedicated', 4), 
             ('who', 3), ('us', 3), ('they', 3), ('so', 3), ('shall', 3), ('people', 3), 
             ('is', 3), ('great', 3), ('dead', 3), ('are', 3), ('It', 3), 
             ('which', 2), ('what', 2)]


#first we set words in alphabetical order
word_list2 = sorted(word_list, key=lambda l: l[0].lower())

# then we sort them by frequency
word_list2 = sorted(word_list2, key=itemgetter(1), reverse = True)

print(word_list2)

The result is:

[('dedicated', 4), ('in', 4), ('this', 4), ('are', 3), ('dead', 3), ('great', 3), ('is', 3), ('It', 3), ('people', 3), ('shall', 3), ('so', 3), ('they', 3), ('us', 3), ('who', 3), ('what', 2), ('which', 2)]

This is so called a complex sort. More here. And it works, because sort operations in python are stable. It means that:

when multiple records have the same key, their original order is preserved.

Sign up to request clarification or add additional context in comments.

3 Comments

yep, I actually just figured it out myself as well. Thank you, you are correct sir. I do have one question, why did you include 'key=lambda' in the first sort?
I did lambda to sort using lower letters, as you have mixture of uppercase and lowercase in your words. Without this, sorting is not as it should be, unless you exactly want that.
If we're using Python 3 we can use casefold instead of lower. (And in this case we don't really need to sort twice, because we could sort by a tuple and flip the sign of the frequency, but that's hard to pull off in the general case.)
1

I would use two sorting functions. One for sorting as you do already. Then sort the results of that by alphabetical order. Does that make sense to you?

word_list = [('this', 4), ('in', 4), ('dedicated', 4), ('who', 3), ('us', 3), ('they', 3), ('so', 3), ('shall', 3), ('people', 3), ('is', 3), ('great', 3), ('dead', 3), ('are', 3), ('It', 3), ('which', 2), ('what', 2)]

word_list.sort(key=lambda i:i[1], reverse = True)
word_list.sort(key=lambda i:i[0].lower())

Comments

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.