๐Ÿ
B.TECH I YEAR โ€ข II SEMESTER โ€ข R25

Python Programming - Lab Manual

Python Programming Laboratory โ€ข Program Solution

๐Ÿ“˜ R25 Regulation ๐ŸŽ“ I Year ๐Ÿ“š II Semester ๐Ÿ Python Programming ๐Ÿงช Lab Program
Selected Program

find the word with most number of occurrences

โ† All Python Programs

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.
โ† Back to Python Lab Programs ๐Ÿ“š All Lab Manuals