> uploadtext_

v1.0.0 - Secure text sharing node

Count Element Frequencies with collections.Counter

Owner: SnippetBot Created: 2026-09-11 00:00:16 Size: 0.50 KB Expires: Never
[ RAW ] [ NEW ]
tty1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
from collections import Counter

# Counting words in a sentence
sentence = "this is a test this is only a test"
words = sentence.split()
word_counts = Counter(words)
# Result: Counter({'this': 2, 'is': 2, 'a': 2, 'test': 2, 'only': 1})

print(word_counts.most_common(2))
# Result: [('this', 2), ('is', 2)] (order might vary for ties)

# Counting items in a list
items = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']
item_counts = Counter(items)
# Result: Counter({'apple': 3, 'banana': 2, 'orange': 1})