counter
from collections import Counter # Example 1: Basic usage of Counter # A Counter is a dictionary subclass that counts the occurrences of elements. sample_list = ['apple', 'banana', 'apple', 'orange', 'banana', 'banana'] counter = Counter(sample_list) print("Counter object:", counter) # Output: Counter({'banana': 3, 'apple': 2, 'orange': 1}) # Example 2: Accessing individual counts print("Count of 'apple':", counter['apple']) # Output: 2 print("Count of 'grape':", counter['grape']) # Output: 0 (default for non-existing keys) # Example 3: Most common elements (returns a list of (element, count) tuples) print("Most common elements:", counter.most_common(2)) # Output: [('banana', 3), ('apple', 2)] # Example 4: Update the counter with additional data additional_data = ['apple', 'grape', 'orange'] counter.update(additional_data) print("Updated Counter:", counter) # Output: Counter({'banana': 3, 'apple': 3, 'orange': 2, 'grape': 1}) # Example 5: Subtract elements from another counter or iterable counter.subtract(['banana', 'apple']) print("Counter after subtraction:", counter) # Output: Counter({'banana': 2, 'apple': 2, 'orange': 2, 'grape': 1}) # Example 6: Elements method (returns an iterator of repeated elements) elements = list(counter.elements()) print("Elements (expanded):", elements) # Output: ['apple', 'apple', 'banana', 'banana', 'banana', 'orange', 'orange', 'grape'] # Example 7: Clearing the counter (removes all items) counter.clear() print("Counter after clear:", counter) # Output: Counter() # Example 8: Using Counter with a string string = "hello world" counter = Counter(string) print("Counter for the string:", counter) # Output: Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1}) # Example 9: Arithmetic operations (add, subtract, etc.) counter1 = Counter(a=3, b=2, c=1) counter2 = Counter(a=1, b=1, c=1, d=4) # Addition of counters counter3 = counter1 + counter2 print("Counter after addition:", counter3) # Output: Counter({'a': 4, 'b': 3, 'c': 2, 'd': 4}) # Subtraction of counters counter4 = counter1 - counter2 print("Counter after subtraction:", counter4) # Output: Counter({'a': 2, 'b': 1}) # Intersection of counters (minimum counts) counter5 = counter1 & counter2 print("Intersection of counters:", counter5) # Output: Counter({'a': 1, 'b': 1, 'c': 1}) # Union of counters (maximum counts) counter6 = counter1 | counter2 print("Union of counters:", counter6) # Output: Counter({'a': 3, 'b': 2, 'c': 1, 'd': 4}) # Example 10: Using Counter with a defaultdict (helpful in some cases) from collections import defaultdict counter = defaultdict(int) # Increment the count of elements counter['apple'] += 1 counter['banana'] += 2 counter['apple'] += 3 print("Counter using defaultdict:", counter) # Output: defaultdict(, {'apple': 4, 'banana': 2}) # Example 11: Converting Counter back to a regular dictionary normal_dict = dict(counter) print("Converted to regular dictionary:", normal_dict) # Output: {'apple': 4, 'banana': 2}

from collections import Counter
# Example 1: Basic usage of Counter
# A Counter is a dictionary subclass that counts the occurrences of elements.
sample_list = ['apple', 'banana', 'apple', 'orange', 'banana', 'banana']
counter = Counter(sample_list)
print("Counter object:", counter)
# Output: Counter({'banana': 3, 'apple': 2, 'orange': 1})
# Example 2: Accessing individual counts
print("Count of 'apple':", counter['apple']) # Output: 2
print("Count of 'grape':", counter['grape']) # Output: 0 (default for non-existing keys)
# Example 3: Most common elements (returns a list of (element, count) tuples)
print("Most common elements:", counter.most_common(2))
# Output: [('banana', 3), ('apple', 2)]
# Example 4: Update the counter with additional data
additional_data = ['apple', 'grape', 'orange']
counter.update(additional_data)
print("Updated Counter:", counter)
# Output: Counter({'banana': 3, 'apple': 3, 'orange': 2, 'grape': 1})
# Example 5: Subtract elements from another counter or iterable
counter.subtract(['banana', 'apple'])
print("Counter after subtraction:", counter)
# Output: Counter({'banana': 2, 'apple': 2, 'orange': 2, 'grape': 1})
# Example 6: Elements method (returns an iterator of repeated elements)
elements = list(counter.elements())
print("Elements (expanded):", elements)
# Output: ['apple', 'apple', 'banana', 'banana', 'banana', 'orange', 'orange', 'grape']
# Example 7: Clearing the counter (removes all items)
counter.clear()
print("Counter after clear:", counter)
# Output: Counter()
# Example 8: Using Counter with a string
string = "hello world"
counter = Counter(string)
print("Counter for the string:", counter)
# Output: Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1})
# Example 9: Arithmetic operations (add, subtract, etc.)
counter1 = Counter(a=3, b=2, c=1)
counter2 = Counter(a=1, b=1, c=1, d=4)
# Addition of counters
counter3 = counter1 + counter2
print("Counter after addition:", counter3)
# Output: Counter({'a': 4, 'b': 3, 'c': 2, 'd': 4})
# Subtraction of counters
counter4 = counter1 - counter2
print("Counter after subtraction:", counter4)
# Output: Counter({'a': 2, 'b': 1})
# Intersection of counters (minimum counts)
counter5 = counter1 & counter2
print("Intersection of counters:", counter5)
# Output: Counter({'a': 1, 'b': 1, 'c': 1})
# Union of counters (maximum counts)
counter6 = counter1 | counter2
print("Union of counters:", counter6)
# Output: Counter({'a': 3, 'b': 2, 'c': 1, 'd': 4})
# Example 10: Using Counter with a defaultdict (helpful in some cases)
from collections import defaultdict
counter = defaultdict(int)
# Increment the count of elements
counter['apple'] += 1
counter['banana'] += 2
counter['apple'] += 3
print("Counter using defaultdict:", counter)
# Output: defaultdict(, {'apple': 4, 'banana': 2})
# Example 11: Converting Counter back to a regular dictionary
normal_dict = dict(counter)
print("Converted to regular dictionary:", normal_dict)
# Output: {'apple': 4, 'banana': 2}