4

I have a dictionary with key as words and values as ints.

Is it possible to sort the dictionary by values?

I want to be able to take the top 10 most occurring words in my dictionary. The values represent the word counts and the keys represent the word.

counter = 9
for a,b in sorted(dict_.iteritems()):
        if counter > 0:
            print str(a),str(b)+"\n"
            counter-=1

This is what i have so far but it is only printing off the first 10 items in the dictionary. How would I print off the top 10 most frequent items? (ie The values with the highest int as the value?)

5 Answers 5

2

Try sorted(dict_.iteritems(), key=lambda item: -item[1]).

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

3 Comments

@khachik: It's to reverse the list.
@nmichaels: Then it should be mapped back by lambda x: (x[0], -x[1])? Why not sorted(..., key=..., reverse=True)?
Negating the key and adding reverse=True have equivalent effects. There is no effect on the sorted items that are returned, only their order. I used the negative key because it's my personal stylistic preference.
2

Use

sorted(dict_.iteritems(), key=lambda x:x[1]) 

or

import operator
sorted(.... key=operator.itemgetter(1)) 

to sort based on element values. You can use the reverse=True argument to invert the order of the results (default oder is ascending values) and slice notation (results[:10]) to iterate only the first 10 elements. You can also omit the reverse flag and use [-10:] to get the top 10.

1 Comment

Ahhh, I love me some operator.
2

Python dictionaries are unordered, but you can convert it to a list of tuples using items() and pass an appropriate comparison function to sort's key parameter.

sorted() has an analogous key parameter. You'd want to sort by lambda item: item[1] to get the value out of items() and iteritems(). Then you can just slice off the first N items.

So...

for a, b in sorted(dict_.iteritems(), key=lambda item: item[1], reverse=True)[:10]:
    print a, b

2 Comments

Isn't this now for a, b in sorted(dict_.iteritems(), key=lambda item: item[1], reverse=True)[:10]:
@MichaelTomkins: It is! 2011 me must have missed that.
1

You can't sort dicts at all. They're unordered, i.e. the order is undefined and completely meaningless (for you).

However, you can sort .iteritems() with key=operator.itemgetter(1) (other answers negate the value, but you can just use the slice [-10:] to get the last 10 items). Or, in this particular case, just use collections.Counter, which comes with a .most_common(n) method.

1 Comment

dicts are unordered is obvious but the question is that is it possible to sort it on values.
0

In order to do that you should sort it using the key argument. key must be a function that takes an element as input and returns another that should be sortable and it will sort the whole elements using this key. And take the last 10 elements (it is sorted in ascending order). In your case you will need to do something like this:

for a,b in sorted(key=lambda x: (x[1], x[0]), dict_.iteritems())[-10:]:
    print str(a), str(b)

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.