201k views
3 votes
Write a function called word_count that takes a string and returns a dictionary that maps every case-insensitive word in the string to the number of times it occurs in the string. Don't worry about punctuation, just count it as part of the word it's attached to.

User Feanaro
by
4.4k points

1 Answer

5 votes

Solution :

We need to write a function named ''
$\text{word}$_
$\text{count}$". The program takes a string and then returns a dictionary which maps the case insensitive word.

def
$\text{word}$_
$\text{count}$(s):


$\text{words}$ =
$\text{s.lower}$().split()


$\text{result}$ = {}

for
$x$ in words:

if
$x$ in
$\text{result}$.keys():


$\text{result}$[x] += 1

else:


$\text{result}$[x] = 1

return
$\text{result}$

User Kwhohasamullet
by
4.2k points