PYTHON
Counting Frequencies with `collections.Counter`
Utilize `collections.Counter` in Python to quickly count hashable objects, perfect for frequency analysis, identifying common elements, or generating tag clouds from textual data.
from collections import Counter
def count_item_frequencies(items):
# Count frequencies of all items in a list
item_counts = Counter(items)
return item_counts
def find_most_common_words(text, num_words=3):
# Count word frequencies and find N most common
words = text.lower().split()
word_counts = Counter(words)
return word_counts.most_common(num_words)
# Example Usage
fruits = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple', 'grape']
fruit_frequency = count_item_frequencies(fruits)
print(f"Fruit frequencies: {fruit_frequency}")
# Expected: Counter({'apple': 3, 'banana': 2, 'orange': 1, 'grape': 1})
votes = ['candidate A', 'candidate B', 'candidate A', 'candidate C', 'candidate B', 'candidate A']
vote_frequency = count_item_frequencies(votes)
print(f"Vote frequencies: {vote_frequency}")
print(f"Most common vote: {vote_frequency.most_common(1)}")
sentence = "This is a test sentence. This sentence is a test. Python is great."
most_common = find_most_common_words(sentence, 2)
print(f"2 most common words: {most_common}")
# Expected: [('is', 3), ('a', 2)] (depending on tokenization, 'sentence' might also be 2)
How it works: `collections.Counter` is a specialized dictionary subclass designed for counting hashable objects. It's incredibly efficient for frequency analysis, allowing you to easily tally occurrences of items in a list, words in a text, or any other collection of data. Its `most_common()` method is particularly useful for quickly extracting the top N most frequent items, making it perfect for tasks like generating leaderboards, tag clouds, or analyzing categorical data in web applications.