find the word with most number of occurrences
Aim:
☛ Write a Python code to Read text from a text file, find the word with most number of occurrences.
Solution :
Input Data file ("data.txt"):
This is Study Glance. Study Galnce provides various Study related meterials.
PROGRAM: (most_word.py)
count = 0
word = " "
max_Count = 0
words = []
#Opens a file in read mode
file = open("data.txt" , "r")
#Gets each line till end of file is reached
for line in file :
#Splits each line into words
string = line.lower().replace(',',' ').replace('.','').split(" ");
#Adding all words generated in previous step into words
for s in string:
words.append(s)
#Determine the most repeated word in a file
for i in range(0,len(words)):
count = 1
#Count each word in the file and store it in variable count
for j in range(i+1 , len(words)):
if(words[i] == words[j]):
count = count + 1
# If maxCount is less than count then store value of count in maxCount
# and corresponding word to variable word
if (count > max_Count):
max_Count = count
word = words[i]
print(" Most repeated word is = " + word)
file.close()
OUTPUT:
Most repeated word is = study
๐ป Run This Python Program
find the word with most number of occurrences
The selected Python program is loaded
automatically into the editor.
Edit the program, provide input if required
and click Run.
The compiler is embedded using OneCompiler.
Your Python program runs inside the embedded
Python environment; Study Glance does not execute
Python code directly on your web server.
Related Python Lab Programs
๐
Python Basics
1.
Use a web browser to go to Python website http://python.org. Start Python interpreter and type help().
View Solution โ
3.
Write a Python program to calculate compound interest for given principal amount, rate of interest and number of periods.
View Solution โ
4.
Write a Python program to read name, address, email and phone number through keyboard and print them.
View Solution โ
6.
Write a Python program to check whether the given input is a digit, lowercase letter, uppercase letter or special character.
View Solution โ
7.
Write a Python program to print all prime numbers in a given interval using break.
View Solution โ
๐
Arrays, Lists and Tuples
4.
Write a Python function is_sorted() to check whether the elements of a list are sorted.
View Solution โ
5.
Write a Python function has_duplicates() to check whether a list contains duplicate elements.
View Solution โ
6.
Write a Python function remove_duplicates() to remove duplicate elements from a list.
View Solution โ
๐ค
Strings and Dictionaries
1.
Write a Python program to add single-letter words such as I, a and an empty string to a given word list.
View Solution โ
3.
Write a Python program to add a comma between every character of a string. Example: Apple โ A,p,p,l,e.
View Solution โ
4.
Write a Python program to remove a given word from all occurrences in a string.
View Solution โ
5.
Write a Python function to capitalize the first letter of each word in a sentence and convert the remaining letters to lowercase without built-in functions.
View Solution โ
๐
Recursion and Matrices
1.
Write a recursive Python function to generate all binary strings of n-bit length.
View Solution โ
โ๏ธ
Modules, Exceptions and Classes
1.
Create a Python module and demonstrate its use with geometric shapes and operations.
View Solution โ
3.
Define draw_rectangle(Canvas, Rectangle) and write a Python program to draw a rectangle.
View Solution โ
4.
Add a color attribute to the Rectangle class and modify draw_rectangle() to draw a colored rectangle.
View Solution โ
6.
Create a Circle class and write a Python program with draw_circle() to draw a circle.
View Solution โ
๐
Validation and File Handling
1.
Write a Python program to read phone number and email address and validate them.
View Solution โ
3.
Write a Python program to open a file, check whether specified words are present and display the words when found.
View Solution โ
4.
Write a Python program to read a text file and find the word with the most number of occurrences.
Current
5.
Write a Python program to find the number of words, vowels, blank spaces, lowercase letters and uppercase letters in a text file.
View Solution โ
๐
Python Libraries, Logic Gates and GUI
3.
Write a Python program to implement Digital Logic Gates: AND, OR, NOT and EX-OR.
View Solution โ
4.
Create a GUI window wizard with two text labels, two text fields, Submit and Reset buttons.
View Solution โ