How to use counter in python 3.10
How to use counter in python 3.10
Welcome everyone, Today we will see what is counter in python-How to accessing-Arithmetic count. so let’s start:
What is Python Counter?
Python counter class is subclass of dictionary and it is a part of collection modules. we know that python counter is like as container which select track to how many times equivalent values are added.python counter count all positive, zero or negative integer value.
![]() |
How to use counter in python 3-How to accessing-Arithmetic |
how to make a counter in python and How to Initializing :
python Counter support three forms of initialization:
- sequence of item
- dictionary containing key
- counts, or with using keyword argument mapping string names to count.
import collections print (collections.Counter(['x', 'y', 'z', 'x', 'x', 'y'])) print (collections.Counter({'y': 2, 'x': 3, 'z':1})) print(collections.Counter(y=2, x=3, z=1))
The output shows all three form of initialization are the same –
Counter({'x': 3, 'y': 2, 'z': 1}) Counter({'x': 3, 'y': 2, 'z': 1}) Counter({'x': 3, 'y': 2, 'z': 1})
In the next example we see
- To create an empty counter
- pass the counter with no argument
- populate it via the update method.
import collections sach = collections.Counter() print('Initial: ', sach) sach.update('loveloo') print('Sequence: ', sach) sach.update({'l': 3, 'o':2}) print('Dict: ', sach)
Output
Initial: Counter() Sequence: Counter({'o': 3, 'l': 2, 'v': 1, 'e': 1}) Dict: Counter({'l': 5, 'o': 5, 'v': 1, 'e': 1})
how to add a counter in python and How to Accessing Count
following way to accessing it.
import collections sach = collections.Counter('lovelloo') for letter in 'loveyou': print('%s : %d' %(letter, sach[letter]))
Output
l : 3 o : 3 v : 1 e : 1 y : 0 u : 0
elements() method in python
it’s return an iterator those produces all of the item (Counters).
import collections sach = collections.Counter('loveyouso') sach['z'] = 0 print(sach) print(list(sach.elements()))
Output
Counter({'o': 3, 'l': 1, 'v': 1, 'e': 1, 'y': 1, 'u': 1, 's ': 1})
we use most_common() functions to produce n input and their respective count. In this the order of the element is not fixed.
import collections sach = collections.Counter() sent = '''Love blur my vision; but after recede it, me can see more clearly than ever– sachin pagar ''' for word in sent:sach.update(word.rstrip().lower()) print("show Three most common letters in the above sent: ") for letter, count in sach.most_common(5): print("%s: %7d" %(letter, count))
Output
show Three most common letters in the above sent:
e: 11
a: 07
o: 05
we use “Love blur my vision; but after recede it, me can see more clearly than ever– sachin pagar” this example they count the letters from the sentence to produce a frequency distribution, then prints theThree most common.
Arithmetic
It is possible to support arithmetic and set operations in counter.
import collections sach1 = collections.Counter(['a', 'b', 'a''c', 'a' ,'b', 'b']) sach2 = collections.Counter('alphabet') print('sach1: ', sach1) print('sach2: ', sach2) print ('n get result Combined counts: ') print(sach1 + sach2) print('n get result Subtraction: ') print(sach1 - sach2) print('n get Intersection result:') print(sach1 & sach2) print('n get Union result:') print(sach1 | sach2)
Output
sach1: Counter({'b': 3, 'a': 3, 'c': 1}) sach2: Counter({'a': 3, 'l': 1, 'p': 1, 'h': 1, 'b': 1, 'e': 1, 't': 1}) get result Combined counts: Counter({'a': 5, 'b': 4, 'c': 1, 'l': 1, 'p': 1, 'h': 1, 'e': 1, 't': 1}) get result Subtraction: Counter({'b': 2, 'c': 1}) get Intersection result: Counter({'a': 3, 'b': 1}) get Union result: Counter({'b': 3, 'a': 5, 'c': 1, 'l': 1, 'p': 1, 'h': 1, 'e': 1, 't': 1})