Saved Bookmarks
| 1. |
Write a program to count the occurrences of each word in a given string? |
|
Answer» def word_count(str): counts = dict ( ) words = str.split ( ) for word in words: if word in counts: counts[word] +=1 else: counts[word]=1 return counts print (word_count (‘the quick brown fox jumps over the lazy dog.’)) Ouput: {‘the’: 2, ‘jumps’: 1, ‘brown’: 1, ‘lazy’: 1, ‘fox’: 1, ‘over’: 1, ‘quick’: 1, ‘dog’: 1} |
|