How to use counter in python 3-How to accessing-Arithmetic

How to use counter in python 3-How to accessing-Arithmetic :

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 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})
Some FAQ about counter in python :
What is a counter in Python?
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 do you write a counter in Python?
 It is always remember that Counter support three forms of initialization steps:sequence of item in text,dictionary containing key and counts them and using keyword arguments.
How do you count occurrences in Python?
we simply use list. count  method to count the occurrences in python.
How do you create a set in Python?
Inside the curly braces {} placing all the element and separated by comma, or by using the built-in set() function we create the set 
Summary:
In this article we mostly covered all the counter related question and there answer. If about this section any query then comment please.
Tags: How to use  counter in python 3-How to accessing them-Arithmetic operation in python counter, How do you write a counter in Python,How do you count occurrences in Python,How do you create a set in Python?
If you leant to learn free python crash course then click on python tutorials and if you like this post then please comment and share.
Best of luck !!!

Previous
Next Post »

comparison python vs r vs sas vs spss

python  vs r vs sas vs spss : Welcome everyone, Today we will see comparison between  r vs python vs sas vs spss . So Let's start : ...

Get Free Crash Course Subscription Valid Only For 10000 Subscriber.

Enter your email address:

Delivered by FeedBurner