Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: double sort

Tags:

python

sorting

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.

like image 593
ACx Avatar asked Mar 27 '15 02:03

ACx


People also ask

How do you sort a double list in Python?

How do you sort a 2D list in Python using lambda? Use sorted() with a lambda function to sort a multidimensional list by column. Call sorted(iterable, key=None) with key set to a lambda function of syntax lambda x: x[i] to sort a multidimensional list iterable by the i th element in each inner list x .

How do you sort two times in Python?

@moose, @Amyth, to reverse to only one attribute, you can sort twice: first by the secondary s = sorted(s, key = operator. itemgetter(2)) then by the primary s = sorted(s, key = operator. itemgetter(1), reverse=True) Not ideal, but works.

Can you sort by two keys Python?

How do you sort by two keys in Python? Use a lambda function with a tuple to sort with two keys Call sorted(iterable, key: NoneType=None) with a collection as iterable . For key , create a lambda function that takes an element as a parameter and returns a 2-tuple of mapped to values.

How do you sort a multidimensional list in Python?

sort , sorted accept optional key parameter. key function is used to generate comparison key. >>> sorted(lst, key=lambda x: x[1], reverse=True) [['I', 219], ['A', 22], ['P', 14], ['V', 13], ['G', 10], ...] >>>


1 Answers

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.

like image 54
Marcin Avatar answered Oct 24 '22 19:10

Marcin