Created
September 3, 2020 14:00
-
-
Save sheetaldubal/7e5cb0f9f294e67c4d1f09ea46569c67 to your computer and use it in GitHub Desktop.
Split the string sentence into a list of words, then create a dictionary named word_counts that contains each word and the number of times it occurs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sentence = "The dog chased the rabbit into the forest but the rabbit was too quick." | |
words=[] | |
word_counts={} | |
for i in sentence.split(): | |
words.append(i) | |
for c in words: | |
if c not in word_counts: | |
word_counts[c] = 0 | |
word_counts[c] = word_counts[c] + 1 | |
print(word_counts) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sentence = "The dog chased the rabbit into the forest but the rabbit was too quick."
word_counts= {}
for i in sentence.split():
if i not in word_counts:
word_counts[i]= 0
word_counts[i]+=1
print(word_counts)