Juicy
Juicy

Reputation: 12520

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

Upvotes: 6

Views: 10814

Answers (4)

sjas
sjas

Reputation: 19667

python3:

sorted_words=sorted(words.items(),key=lambda item: int(item[1]))

Upvotes: 5

Steinar Lima
Steinar Lima

Reputation: 7821

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.

Upvotes: 6

kjo
kjo

Reputation: 35301

For this sort of thing, I tend to do something like

sorted_pairs = sorted(words.iteritems(), key=lambda p: p[1])

The above assumes that the values in words are indeed numbers. If this is not the case, then I'd do

sorted_pairs = sorted(words.iteritems(), key=lambda p: float(p[1]))

(The reason for using float instead of int in the last expression is simply generality: the code stays the same even if your values start including floating point numbers.)

Upvotes: 1

Slater Victoroff
Slater Victoroff

Reputation: 21914

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.

Upvotes: 2

Related Questions