Tom
Tom

Reputation: 11

Rank keys of dict with lists

I have a dictionary referring to a set of documents. Each document is associated with the frequencies of 5 specific words, which I captured in a list for each value in the dict. The dict looks something like this:

docs = {'doc1': [144, 91, 1, 28, 130], 'doc2': [3, 4, 21, 59, 319], 'doc3': [4, 121, 12, 14, 23]}

Now, I'm looking to rank the keys in this dict based on these word frequencies. Specifically, I want to rank them five times (for each of the five values). So doc1 comes out on top in ranking 1, doc3 comes out on top in ranking 2, etc.

I then want to produce a final ranking of the docs, which takes the average rank per doc and then ranks the averages.

I'm new to Python and getting a bit lost in sorted dicts, addressing lists in dicts, etc. How would I go about doing this?

Upvotes: 0

Views: 1160

Answers (1)

John Zwinck
John Zwinck

Reputation: 249333

First, make the full list of names:

names = list(docs.keys()) # list() not needed in Python 2

Then, sort it using custom criteria:

sorted(names, key=lambda name: docs[name][0], reverse=True)

Or for all 5 at once:

[sorted(names, key=lambda name: docs[name][ii], reverse=True) for ii in range(5)]

Which gives you:

[['doc1', 'doc3', 'doc2'],
 ['doc3', 'doc1', 'doc2'],
 ['doc2', 'doc3', 'doc1'],
 ['doc2', 'doc1', 'doc3'],
 ['doc2', 'doc1', 'doc3']]

As for the overall ranking, you could rank them by total counts like this:

sorted(names, key=lambda name: sum(docs[name]), reverse=True)

Upvotes: 1

Related Questions