Answer to Question #166090 in Python for hemanth

Question #166090

Word Count - 2

Given a sentence S, write a program to print the frequency of each word in S, where words are sorted in alphabetical order.


Input


The input will be a single line containing a string S.


Output


The output contains multiple lines, with each line containing a word and frequency of each word in the given string separated by ": ", where words are sorted in alphabetical order.


Explanation


For example, if the given sentence is "Hello world, welcome to python world", the output should be

Hello: 1

python: 1

to: 1

welcome: 1

world: 2


1
Expert's answer
2021-02-26T02:55:35-0500
words = {}
for str in input().split():
    word = ("".join([ c if c.isalnum() else "" for c in str ]))
    if word not in words:
        words[word] = 1
    else:
        words[word] += 1

for word in sorted(words):
    print(word, ": ", words[word], sep='')

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
APPROVED BY CLIENTS