You are on page 1of 2

# Python program to count the frequency of words

# from a file..

fh = open ("demo2.txt","r") #File Handler

cont = fh.read() #Content stored in string format into cont variable

lst = cont.split()

#Each word of the content stored into

#the variable lst in list format

#(note: each word is string but stored into list lst)

nwlst = []

# Created new list to store the words of the old list

# after converting them to uppercase

for j in range(len(lst)):

nwlst.append(lst[j].upper())

# converting each of the list 'lst' to uppercase and,

# storing them to new list 'nwlst'

eplst = []

# Created new list to pass counted variable into it

# So, that there is no same frequency displayed twice or more.

for i in nwlst: # Picking each word from the capitalized list.

if i not in eplst: # Checking if its already have beem counted.

c = nwlst.count(i) # Counting the frequency of the words.

print("Frequency of ",i," = ",c) # Printing the frequency of the worfs.

eplst.append(i) # storing the counted words into the list.


# NOTE: Use of eplst has been described above.

fh.close() #closing the File Handler

You might also like