The program takes as an input a text file. Your program should print all the unique words in the file and their frequencies in alphabetical order.
The program takes as an input a text file. Your program should print all the unique words in the file and their frequencies in alphabetical order
# Python code to find frequency of each word
def freq(string):
# break the string into list of words
string = string.split()
str2 = []
# loop till string values present in list str
for i in string:
# checking for the duplicacy
if i not in str2:
# insert value in str2
str2.append(i)
for i in range(0, len(str2)):
# count the frequency of each word(present
# in str2) in str and print
print('Frequency of', str2[i], 'is :', string.count(str2[i]))
def main():
string=''
with open("unique.txt",'r') as f:
for line in f:
string += line
freq(string)
if __name__=="__main__":
main() # call main function Print this post
# Python code to find frequency of each word
def freq(string):
# break the string into list of words
string = string.split()
str2 = []
# loop till string values present in list str
for i in string:
# checking for the duplicacy
if i not in str2:
# insert value in str2
str2.append(i)
for i in range(0, len(str2)):
# count the frequency of each word(present
# in str2) in str and print
print('Frequency of', str2[i], 'is :', string.count(str2[i]))
def main():
string=''
with open("unique.txt",'r') as f:
for line in f:
string += line
freq(string)
if __name__=="__main__":
main() # call main function Print this post
Comments
Post a Comment