How to get the top "n" most frequently used words from list?

Viewed 513

I have two lists. Each list includes words. Some words are common to both lists, some are not. I want to output only 20 most frequently common words but my code shows all common words. I want to limit the range to 20. I'm not allowed to use COUNTER.

def countwords(lst):
    dct = {}
    for word in lst:
        dct[word] = dct.get(word, 0) + 1
    return dct


count1 = countwords(finallist1)
count2 = countwords(finallist2)

words1 = set(count1.keys())
words2 = set(count2.keys())

common_words = words1.intersection(words2)
for i,w in enumerate (common_words,1):
    print(f"{i}\t{w}\t{count1[w]}\t{count2[w]}\t{count1[w] + count2[w]}")

Expected output:

common   f1 f2 sum 
1 program 5 10 15 
2 python  2  4  6 
.
.
until 20
1 Answers

You can use .most_common() of collections.Counter to achieve this:

>>> from collections import Counter
>>> word_list = ["one", "two", "three", "four", "two", "three", "four", "three", "four", "four"]

>>> Counter(word_list).most_common(2)
[('four', 4), ('three', 3)]

From the Counter().most_common() documentation :

Return a list of the "n" most common elements and their counts from the most common to the least. If "n" is omitted or None, most_common() returns all elements in the counter. Elements with equal counts are ordered in the order first encountered


Here's an alternative to achieve the same without importing any module:

# Step 1: Create Counter dictionary holding frequency. 
#         Similar to: `collections.Counter()` 
my_counter = {}
for word in word_list:
    my_counter[word] = my_counter.get(word, 0) + 1

# where `my_counter` will hold:
# {'four': 4, 'three': 3, 'two': 2, 'one': 1}
#-------------

# Step 2: Get sorted list holding word & frequency in descending order.
#         Similar to: `Counter.most_common()`
sorted_frequency = sorted(my_counter.iteritems(), key=lambda x: x[1], reverse=True)

# where `sorted_frequency` will hold:
# [('four', 4), ('three', 3), ('two', 2), ('one', 1)]
#-------------

# Step 3: Get top two words by slicing the ordered list from Step 2.
#         Similar to: `.most_common(2)`
top_two = sorted_frequency[:2]

# where `top_two` will hold:
# [('four', 4), ('three', 3)]

Please refer the comments in above code snippet for the step by step explanation.

Related