Skip to content

Instantly share code, notes, and snippets.

@sheetaldubal
Created September 3, 2020 14:00
Show Gist options
  • Save sheetaldubal/7e5cb0f9f294e67c4d1f09ea46569c67 to your computer and use it in GitHub Desktop.
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.
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)
@MustafaCoder2022
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment