In: Computer Science
The assignment is to build a program in Python that can take a string as input and produce a “frequency list” of all of the wordsin the string (see the definition of a word below.) For the purposes of this assignment, the input strings can be assumed not to contain escape characters (\n, \t, …) and to be readable with a single input() statement.
When your program ends, it prints the list of words. In the output, each line contains of a single word and the number of times that word occurred in the input. For readability, the number should be the first thing on the line and the word should be second.
For example, here are two runs of the program, showing the user’s input and the output:
Enter a line of text: This is a very long line of text with many words in it, most of them only once.
1 this
1 is
1 a
1 very
1 long
1 line
2 of
1 text
1 with
1 many
1 words
1 in
1 it
1 most
1 them
1 only
1 once
Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate
the question. Thank You So Much.
def Count(array):
uniqueWord = dict();
for word in array:
if word in uniqueWord:
uniqueWord[word] =uniqueWord[word]+1;
else:
uniqueWord[word] = 1;
return uniqueWord
if __name__ == '__main__':
array = []
str = input("Enter a line of text: ")
array = str.split(" ")
uniqueWord=Count(array)
for key in uniqueWord:
print("%d %s"%(uniqueWord[key],key))