Counters in python

In this tutorial, we will discuss the counter that included in the collection module. we will also explain how we can use it to solve problems.

we will also learn how to count several repeated objects at once, creating counters, retrieve the most common objects in a counter and  updating objects counts.

Counter is a sub-class that is used to count hash table objects. It creates a hash table of an iterable when called. Counter class is a special type of object data=set provided with the collections module in python. Collections module provides the user with specialized container datatypes such as dictionaries, lists, and tuples.

What is counter?

counters are known as the container that holds the count of each of the elements that are available in the container. they allow us to access the stored object. the dictionary class holds the counter as the sub-class.

SYNTAX:

class collections.Counter([iterable-or-mapping])

Why do we use counter?

  • The counter is used to store the data in the unordered collection, the same as hash table objects.
  • The elements are represented as key, and counts are values.
  • we can perform the basic arithmetic operations such as addition, subtraction and union one the counter.
  • A counter is also capable of counting elements from another counter.

Implementation of Counters

python takes iterable objects like list, dictionary, tuple, string as an argument and returns the count of each element.

from collections import Counter
List1 = ['a', 'b', 'a', 'a', 'b', 'c', 'v', 'v', 'v', 't', 'b', 't', 't']
# Print count of the variable 
print(Counter(List1))

Output:

Counter({'a' : 3, 'b' : 3, 'v' : 3, 't' : 3, 'c' : 1})

Updating counter

The collection module provides the update() method which allows us to update or add value.

from collections import Counter
_count= Counter()
_count.update = ('vallapuneni Akshara!')
print(_count)

Output:

Counter({'a' : 5, 'l' : 2, 'n' : 2, 'v' : 1, 'p' : 1, 'u' : 1, 'e' : 1, 'i' : 1, 'k' : 1, 's' : 1, 'h' : 1, 'r' : 1, '!' : 1})

Accessing counter

we can access the values from the Counter.

from collections import Counter
count_num = Counter()
count_num.update('Welcome to Akshara Tutorials!')
print('%s : %d' % ('u', count_num['T']))
print('\n')
for char in 'Tutorials':
    print('%s : %d' % (char, count_num[char]))

Output:

u  :  2



T  :  2
u  :  1
t  :  3
o  :  4
r  :  1
i  :  2
a  :  3
l  :  2
s  :  1

Deleting an Element from Counter

The del keyword is used to delete the element from the counter.

from collections import Counter
dict1 = {'a': 3, 'b': 1, 'c': 8}
del dict1["c"]
print("Dictionary After Deletion:",Counter(dict1))

Output:

Dictionary After Deletion: Counter({'a' : 3, 'b' : 1})

Counter with string

Strings are enclosed character in characters in the double quotes.

from collections import Counter
str1 = "Vallapuneni Akshara"
print(Counter(str1))

Output:

Counter({'a' : 5, 'l' : 2, 'n' : 2, 'v' : 1, 'u' : 1, 'e' : 1, 'i' : 1, 'k' : 1, 's' : 1, 'h' : 1, 'r' : 1})

Counter with Dictionary

A dictionary stores the elements in the key-value pair define inside the curly brackets.

from collections import Counter
dict1 = {'a': 5, 'b': 6, 'c': 7, 'c':2}
print(Counter(dict1))

Output:

Counter({'b' : 6, 'a' : 5, 'c' : 2})

Counter with Tuple

Tuples are immutable data-structure where elements are stored separated by commas inside round brackets. A tuple is converted into a hash table object.

from collections import Counter
tup1 = ('x','y','z','x','x','x','y','z')
print(Counter(tup1))

output:

Counter({'x' : 4, 'y' : 2, 'z' : 2})

Applications

 

  • Counter object along with its functions are used collectively for processing huge amounts of data.
  • It is one of a vary high processing and functioning tools and can even function with wide range of data.

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top