Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort dictionary by the INT value of the value

There are many posts here about sorting dictionaries in Python so I was careful to read them and hope this is not a duplicate:

I'm using a dictionary to hold words as keys and the occurrence of the word as value. This leads to a dictionary that could be like:

John 1
Robert 5
Susie 15
Alex 6

I want to sort them by occurrence (the 'value')

John 1
Robert 5
Alex 6
Susie 15

I'm using the following code to try to sort my dictionary like this:

sorted_words = sorted(words.iteritems(), key=itemgetter(1))

However this returns a sorted list of tuples that looks like this:

John 1
Susie 15
Robert 5
Alex 6

You can see the problem is that with the above code the values are sorted "alphabetically", so 15 follows 1, even though mathematically 15 > 5 and 6 and so should be last.

How can I fix the code to treat the values as INTs and not strings

like image 409
Juicy Avatar asked Dec 26 '22 13:12

Juicy


2 Answers

You have to convert to values to integers in your key expression. Use

sorted_words = sorted(words.iteritems(), key=lambda x: int(x[1]))

It may be tempting to try something like key=int(itemgetter(1)), but this will not work since the key parameter is expecting a function.

like image 99
Steinar Lima Avatar answered Jan 06 '23 09:01

Steinar Lima


If you're looking for words sorted by occurrence you really want to be using a Counter, which is basically a prebuilt histogram that will handle all of this for you, and even let you call the function most_common to get the most common elements from that dictionary.

from colletions import Counter

string = "There there are some some words here here"
test = Counter(string.split())
>>> test.most_common(2)
[('some', 2), ('here', 2)]

If that doesn't fit your application for some reason, you can (as other have suggested), sort your dictionary as follows:

sorted_words = sorted(words.iteritems(), key=lambda value: float(value[1]))

But a Counter does seem to be a much closer fit for your application.

like image 41
Slater Victoroff Avatar answered Jan 06 '23 09:01

Slater Victoroff