Monday, July 5, 2021

Python's Counter: The Pythonic Way to Count Objects

Counting several repeated objects at once is a common problem in programming. Python offers a bunch of tools and techniques you can use to approach this problem. However, Python’s Counter from collections provides a clean, efficient, and Pythonic solution.

This dictionary subclass provides efficient counting capabilities out of the box. Understanding Counter and how to use it efficiently is a convenient skill to have as a Python developer.

In this tutorial, you’ll learn how to:

  • Count several repeated objects at once
  • Create counters with Python’s Counter
  • Retrieve the most common objects in a counter
  • Update object counts
  • Use Counter to facilitate further computations

You’ll also learn about the basics of using Counter as a multiset, which is an additional feature of this class in Python.

Counting Objects in Python

Sometimes you need to count the objects in a given data source to know how often they occur. In other words, you need to determine their frequency. For example, you might want to know how often a specific item appears in a list or sequence of values. When your list is short, counting the items can be straightforward and quick. However, when you have a long list, counting things can be more challenging.

To count objects, you typically use a counter, which is an integer variable with an initial value of zero. Then you increment the counter to reflect the number of times a given object appears in the input data source.

When you’re counting the occurrences of a single object, you can use a single counter. However, when you need to count several different objects, you have to create as many counters as unique objects you have.

To count several different objects at once, you can use a Python dictionary. The dictionary keys will store the objects you want to count. The dictionary values will hold the number of repetitions of a given object, or the object’s count.

For example, to count the objects in a sequence using a dictionary, you can loop over the sequence, check if the current object isn’t in the dictionary to initialize the counter (key-value pair), and then increment its count accordingly.

Here’s an example that counts the letters in the word “Mississippi”:

>>>
>>> word = "mississippi"
>>> counter = {}

>>> for letter in word:
...     if letter not in counter:
...         counter[letter] = 0
...     counter[letter] += 1
...

>>> counter
{'m': 1, 'i': 4, 's': 4, 'p': 2}

The for loop iterates over the letters in word. In each iteration, the conditional statement checks if the letter at hand isn’t already a key in the dictionary you’re using as counter. If so, it creates a new key with the letter and initializes its count to zero. The final step is to increment the count by one. When you access counter, you see that the letters work as keys and the values as counts.

Another way to count objects with a dictionary is to use dict.get() with 0 as a default value:

>>>
>>> word = "mississippi"
>>> counter = {}

>>> for letter in word:
...     counter[letter] = counter.get(letter, 0) + 1
...

>>> counter
{'m': 1, 'i': 4, 's': 4, 'p': 2}

When you call .get() this way, you get the current count of a given letter, or 0 (the default) if the letter is missing. Then you increment the count by 1 and store it under the corresponding letter in the dictionary.

You can also use defaultdict from collections to count objects within a loop:

>>>
>>> from collections import defaultdict

>>> word = "mississippi"
>>> counter = defaultdict(int)

>>> for letter in word:
...     counter[letter] += 1
...

>>> counter
defaultdict(<class 'int'>, {'m': 1, 'i': 4, 's': 4, 'p': 2})

This solution is more concise and readable. You first initialize the counter using a defaultdict with int() as a default factory function. This way, when you access a key that doesn’t exist in the underlying defaultdict, the dictionary automatically creates the key and initializes it with the value that the factory function returns.

In this example, since you’re using int() as a factory function, the initial value is 0, which results from calling int() without arguments.

Like with many other frequent tasks in programming, Python provides a better way to approach the counting problem. In collections, you’ll find a class specially designed to count several different objects in one go. This class is conveniently called Counter.

Getting Started With Python’s Counter

Read the full article at https://realpython.com/python-counter/ »


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]



from Real Python
read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...