Python Advanced: Counting Frequencies with collections.Counter
This tutorial will demonstrate how to use the `collections.Counter` class in Python to efficiently count the frequencies of items in a list or other iterable. `collections.Counter` is a powerful tool for data analysis and provides a more concise and often faster alternative to manually implementing frequency counting using dictionaries.
Example 1: Basic Frequency Counting
Let’s start with a simple example: counting the frequency of letters in a string.
import collections
text = "this is a test string to test the frequency of letters"
# Use Counter to count the occurrences of each character
letter_counts = collections.Counter(text)
# Print the counts
print(letter_counts)
# Access specific counts
print(f"The frequency of 't' is: {letter_counts['t']}")
print(f"The frequency of 's' is: {letter_counts['s']}")
In this example:
- We import the `collections` module.
- We define a string `text`.
- We create a `Counter` object, passing the string `text` as an argument. This automatically counts the occurrences of each character.
- We print the `letter_counts` object, which is a dictionary-like object where keys are characters and values are their counts.
- We demonstrate how to access specific counts using the key (character) as an index.
Common Mistakes and Corrections:
- Mistake: Attempting to access a key that doesn’t exist without handling the exception.
- Correction: Use `letter_counts.get(‘z’, 0)` to safely retrieve the count of ‘z’ and return 0 if it’s not present, preventing a `KeyError`.
Important Note: The keys in the `Counter` object are the unique items from the input iterable. The values are their corresponding counts.
Output:
Counter({'t': 6, 's': 6, 'i': 5, ' ': 8, 'a': 2, 'e': 6, 'r': 4, 'n': 2, 'g': 3, 'o': 2, 'f': 1, 'c': 1, 'u': 1, 'l': 1, 'd': 1, 'h': 1, 'y': 1, 'v': 1, 'm': 1, 'th': 2})
The frequency of 't' is: 6
The frequency of 's' is: 6
Example 2: Counting Items in a List
Now, let’s count the frequency of elements in a list of numbers.
import collections
numbers = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
# Use Counter to count the occurrences of each number
number_counts = collections.Counter(numbers)
# Print the counts
print(number_counts)
# Find the most common elements
print(f"The most common number is: {number_counts.most_common(1)[0]}")
# Find the top 2 most common
print(f"The top 2 most common numbers are: {number_counts.most_common(2)}")
In this example:
- We import the `collections` module.
- We define a list `numbers`.
- We create a `Counter` object, passing the list `numbers` as an argument.
- We print the `number_counts` object.
- We demonstrate using the `most_common()` method to find the most common element and the top N most common elements.
Output:
Counter({4: 4, 3: 3, 2: 2, 1: 1})
The most common number is: 4
The top 2 most common numbers are: [(4, 4), (3, 3)]
Example 3: Counting Frequencies with a Custom Iterable
Let’s use `Counter` with a generator expression to count the frequency of words in a sentence. This illustrates its flexibility.
import collections
sentence = "this is a sentence this is another sentence"
words = (word for word in sentence.split() for word in word if word.isalpha()) #Generator expression
# Use Counter to count the occurrences of each word
word_counts = collections.Counter(words)
# Print the counts
print(word_counts)
# Find the most common word
print(f"The most common word is: {word_counts.most_common(1)[0]}")
In this example:
- We import the `collections` module.
- We define a sentence.
- We use a generator expression to extract individual words from the sentence and clean up by only including alpha characters.
- We create a `Counter` object, passing the generator expression as an argument.
- We print the `word_counts` object.
- We demonstrate using the `most_common()` method to find the most common word.
Output:
Counter({'this': 2, 'is': 2, 'sentence': 2, 'another': 1})
The most common word is: this



Leave a Reply