Search
 
SCRIPT & CODE EXAMPLE
 

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}
Comment

PREVIOUS NEXT
Code Example
Python :: python for in for in 
Python :: multiple plot in one figure python 
Python :: python logistic function 
Python :: virtual env pyhton 
Python :: how to automatically install python packages 
Python :: float 2 decimals jupyter 
Python :: raw input example python 
Python :: while loop py 
Python :: Change Python interpreter in pycharm 
Python :: dict comprehension 
Python :: how to add custom prefix in discord.py 
Python :: f string add 0 before python 
Python :: get key from value dictionary py 
Python :: python sort algorithm 
Python :: how to split string by list of indexes python 
Python :: keras normalize 
Python :: strip whitespace python 
Python :: python requests post form data 
Python :: string upper lower count python 
Python :: tensorflow inst for python 3.6 
Python :: how to change key to value and versa in python dictionary 
Python :: calculate mean of column pandas 
Python :: list pakages installed in python 
Python :: count number of pages in pdf python pdfminer 
Python :: flask form 
Python :: python anytree 
Python :: python 3 tkinter treeview example 
Python :: binary to octal in python 
Python :: mongodb in python 
Python :: create array of specific size python 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =