We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1
6. Develop a program to print 10 most frequently appearing words in a text file.
[Hint: Use dictionary
with distinct words and their frequency of occurrences. Sort the dictionary in the reverse order of frequency and display dictionary slice of first 10 items]
def count_words(filename):
dict = {}
# Read the file and count the occurrences of each word
with open(filename, 'r') as file:
words = file.read().lower().split()
for word in words:
dict[word] = dict.get(word, 0) + 1
return dict
def top_10_words(dict):
# Sort the dictionary by value in descending order