Is there a way to sort dictionary values with Python

69 Views Asked by At

I'm hoping to create a function that returns a list of the top 5 most common word lengths of a given text, and I'm trying to learn and experiment with defaultdict and Counter specifically.

So far, my code reads as follows:

from collections import defaultdict as dd, Counter

def word_lengths(text):    
    my_dict = dd(int)
    for word in text.split():
        word = len(word)
        my_dict[word]+=1
    return my_dict

def top5(text):
    my_dict = word_lengths(text)
    my_list = sorted(my_dict, key=my_dict.get, reverse=True)[:5]
    return my_list

So far I can tell this works for the most part, however I'd like my final result to be absolutely descending, i.e. if a 6 letter and a 7 letter word were equally as likely, I'd like the 7 to be listed before the 6 in my top 5 list.

So, for "the quick brown fox jumped over a lazy dog", I'd like it to result in [3, 5, 4, 6, 1].

But for "one one was a racehorse two two was one too", it should display [3, 9, 1].

1

There are 1 best solutions below

0
DTNGNR On

If you want to sort words with identical length count (i.e. "racehorse" and "a" in "one one was a racehorse two two was one too") also descending from the length of the word than you need to order words first by length in your function "word_lengths"

def word_lengths(text):    
    my_dict = dd(int)
    words = text.split()
    # prioritize long words first
    for word in sorted(words, key=len, reverse=True):
        my_dict[len(word)]+=1
    return my_dict

With this change it doesn't matter at what position the words "racehorse" or "a" are used in the sentence:

print(top5("one one was a racehorse two two was one too"))

Result: [3, 9, 1]

print(top5("one one was racehorse a two two was one too"))

Result: [3, 9, 1]