Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to Get word frequency in percentage
In this article, we will learn how to calculate word frequency as percentages in Python. Given a list of strings, we'll find what percentage each word represents of the total word count.
Formula
Word Frequency (%) = (Occurrence of word / Total words) × 100
Method 1: Using Counter() from collections
The Counter() function counts hashable objects and returns a dictionary-like object. The join() function connects sequence elements into a single string ?
from collections import Counter
# Input list of strings
input_strings = ["hello tutorialspoint", "python codes", "tutorialspoint for python", "see python codes tutorialspoint"]
print("Input list:")
print(input_strings)
# Join all strings and split into individual words
all_words = " ".join(input_strings).split()
# Count word frequencies
word_counts = Counter(all_words)
total_words = sum(word_counts.values())
# Calculate percentages
word_percentages = {word: (count / total_words) * 100 for word, count in word_counts.items()}
print("\nWord frequency percentages:")
for word, percentage in word_percentages.items():
print(f"{word}: {percentage:.2f}%")
Input list: ['hello tutorialspoint', 'python codes', 'tutorialspoint for python', 'see python codes tutorialspoint'] Word frequency percentages: hello: 9.09% tutorialspoint: 27.27% python: 27.27% codes: 18.18% for: 9.09% see: 9.09%
Method 2: Using count() Function
This approach manually counts each word occurrence using the count() method ?
# Input list of strings
input_strings = ["hello tutorialspoint", "python codes", "tutorialspoint for python", "see python codes tutorialspoint"]
# Join all strings and split into words
all_words = " ".join(input_strings).split()
total_words = len(all_words)
# Calculate percentages using count()
word_percentages = {}
for word in all_words:
if word not in word_percentages:
count = all_words.count(word)
word_percentages[word] = (count / total_words) * 100
print("Word frequency percentages:")
for word, percentage in word_percentages.items():
print(f"{word}: {percentage:.2f}%")
Word frequency percentages: hello: 9.09% tutorialspoint: 27.27% python: 27.27% codes: 18.18% for: 9.09% see: 9.09%
Method 3: Using operator.countOf()
The operator.countOf() function counts occurrences of a value in a sequence ?
import operator as op
# Input list of strings
input_strings = ["hello tutorialspoint", "python codes", "tutorialspoint for python", "see python codes tutorialspoint"]
# Join all strings and split into words
all_words = " ".join(input_strings).split()
total_words = len(all_words)
# Calculate percentages using operator.countOf()
word_percentages = {}
for word in all_words:
if word not in word_percentages:
count = op.countOf(all_words, word)
word_percentages[word] = (count / total_words) * 100
print("Word frequency percentages:")
for word, percentage in word_percentages.items():
print(f"{word}: {percentage:.2f}%")
Word frequency percentages: hello: 9.09% tutorialspoint: 27.27% python: 27.27% codes: 18.18% for: 9.09% see: 9.09%
Comparison
| Method | Time Complexity | Best For |
|---|---|---|
Counter() |
O(n) | Large datasets, built-in optimization |
count() |
O(n²) | Simple cases, no imports needed |
operator.countOf() |
O(n²) | When using operator module features |
Conclusion
Use Counter() for efficient word frequency calculation in large datasets. The count() method works well for smaller lists, while operator.countOf() provides an alternative when working with the operator module.
