Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

how to count repeated words in python

>>> words = "hello this is hello this is baby baby baby baby hello"
>>> words_list = words.split(" ")
>>> words_list
['hello', 'this', 'is', 'hello', 'this', 'is', 'baby', 'baby', 'baby', 'baby', 'hello']

>>> count_dict = dict.fromkeys(set(words_list), 0)
>>> for word in words_list:
...     count_dict[word] += 1
...
>>> count_dict
{'hello': 3, 'baby': 4, 'is': 2, 'this': 2}
 
PREVIOUS NEXT
Tagged: #count #repeated #words #python
ADD COMMENT
Topic
Name
3+3 =