Simple Wordcount in Python
Posted July 27, 2022 by Rohith ‐ 1 min read
Wordcount is like helloword in data engineering.
On this page
It can be simply achieved using Counter
operation from collections
in python.
Using Counter
It can be explained better with an example.
Example:
# create sample data
sample_tuple = ("PYTHON", "PROGRAMMING", "LANGUAGE", "IS", "MODERN", "LANGUAGE")
lower_sample_map = map(lambda e: (e.lower(), 1), sample_tuple) # results iterators
# upon list(lower_sample_map) # [('python', 1), ('programming', 1), ('language', 1), ('is', 1), ('modern', 1), ('language', 1)]
from collections import Counter
# init counter
c = Counter()
for key, value in lower_sample_map:
c[key] += value
print(c)
Output:
Counter({'language': 2, 'python': 1, 'programming': 1, 'is': 1, 'modern': 1})
Convert Counter To Dictionary
Counter object can be used as dictionary. Incase if you want to convert to dictionary,
Example: Extending above example,
dict(c)
Output:
{'language': 2, 'python': 1, 'programming': 1, 'is': 1, 'modern': 1}