Skip to content

Python Counter methods

  • by

There are some important methods available with Python Counter, here is the list of the same:

  • elements(): returns all the elements with a count >0. Elements with 0 or -1 count will not be returned.
  • most_common(value): returns the most common elements from the Counter list.
  • subtract(): used to deduct the elements from another Counter.
  • update(): used to update the elements from another Counter.

Python Counter methods

Simple example code of commonly used methods in Counter.

elements() method – This method returns the count of elements that are greater than 0. The elements with 0 or -1 will be completely omitted.

from collections import Counter

counter1 = Counter({'x': 5, 'y': 2, 'z': -2, 'x1': 0})

_elements = counter1.elements()
for a in _elements:
    print(a)

Output:

Python Counter methods

most_common(value) – It returns the most common elements from the Counter list.

from collections import Counter

C1 = Counter({'x': 5, 'y': 12, 'z': -2, 'x1': 0})

res1 = C1.most_common(2)
print(res1)

res2 = C1.most_common()
print(res2)

Output:

[('y', 12), ('x', 5)]
[('y', 12), ('x', 5), ('x1', 0), ('z', -2)]

subtract() method – It returns the new Counter object after performing subtraction. Let’s understand the following example.

from collections import Counter

c1 = Counter({'x': 5, 'y': 12, 'z': -2, 'x1': 0})
c2 = Counter({'x': 2, 'y': 5})

c1.subtract(c2)
print(c1)

Output: Counter({‘y’: 7, ‘x’: 3, ‘x1’: 0, ‘z’: -2})

update()

from collections import Counter

c1 = Counter({'x': 5, 'y': 12, 'z': -2, 'x1': 0})
c2 = Counter({'x': 2, 'y': 5})

c1.update(c2)
print(c1)

Output: Counter({‘y’: 17, ‘x’: 7, ‘x1’: 0, ‘z’: -2})

Let’s say we have a piece of text, and we want to analyze the frequency of each word, update the counts with another piece of text, and perform various operations.

from collections import Counter

# Initial text
text1 = "this is a sample text with several words this is a test"

# Create a Counter from the first text
counter1 = Counter(text1.split())
print("Initial Counter:", counter1)

# Add counts from another text
text2 = "this is another sample text with some different words"
counter1.update(text2.split())
print("Updated Counter with new text:", counter1)

# Most common words
most_common_words = counter1.most_common(3)
print("Most common words:", most_common_words)

# Subtract counts using another Counter
subtract_text = "this is a sample text"
counter1.subtract(Counter(subtract_text.split()))
print("Counter after subtraction:", counter1)

# Elements in the Counter
elements = list(counter1.elements())
print("All elements in the Counter:", elements)

# Copy the Counter
counter_copy = counter1.copy()
print("Copy of the Counter:", counter_copy)

# Clear the Counter
counter1.clear()
print("Counter after clearing:", counter1)

# Re-create and demonstrate arithmetic operations
counter1 = Counter(text1.split())
counter2 = Counter(text2.split())

# Addition of Counters
addition = counter1 + counter2
print("Addition of two Counters:", addition)

# Subtraction of Counters
subtraction = counter1 - counter2
print("Subtraction of two Counters:", subtraction)

# Intersection of Counters
intersection = counter1 & counter2
print("Intersection of two Counters:", intersection)

# Union of Counters
union = counter1 | counter2
print("Union of two Counters:", union)

This example covers various methods and operations you can perform with the Counter class, providing a comprehensive overview in a real-world context.

Do comment if you have any doubts or suggestions on this Python countertop.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading