Answer to Question #168488 in Python for srikanth

Question #168488
Given a sentence S, write a program to print the frequency of each word in S. The output order should correspond with the word's input order of appearance.
Input

The input will be a single line containing a sentence S.
Output

The output contains multiple lines, with each line containing a word and its count separated by ": " with the order of appearance in the given sentence.
Explanation

For example, if the given sentence is "Hello world, welcome to python world", the output should be 
Hello: 1
world: 2
welcome: 1
to: 1
python: 1
Sample Input 1
Hello world welcome to python world
Sample Output 1
Hello: 1
world: 2
welcome: 1
to: 1
python: 1

Sample Input 2
This is my book
Sample Output 2
This: 1
is: 1
my: 1
book: 1
1
Expert's answer
2021-03-03T06:27:03-0500
# Python code to find frequency of each word
def frequency(statement):
    statement = statement.split()
    statement2 = []

    for i in statement:
        if i not in statement2:
              statement2.append(i)

    for i in range(0, len(statement2)):
          print('Frequency of', statement2[i], 'is :', statement.count(statement2[i]))


def main():
    statement = 'Hello everyone, I hope that you are doing somthing great for the improvement of your knowlede and skills.'
    frequency(statement)

if __name__ == "__main__":
    main()  # call main function

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS