Counter in Python

Counter in Python

Counters is the best thing that has happened to Python when you think as a Data analyst.

Counters can be used as an alternative to a lot of things in python(mainly dictionaries). But rather than telling you when to use it and when not to, I will just tell you what can Counters help you do.

You decide when do you want to use it. :)

  • To use Counters you have to import it from collections module
from collections import Counter

Note : Collections module has some really cool stuff like defaultdict, deque, ordereddict etc. Check it out here.

  • You can define a counter and add data to it like below.
from collections import Counter
e=Counter('red green orange red yellow red'.split())

Note: when you check the type of "e" declared above you will get the expected "class 'collections.Counter'"

>>> type(e)
<class 'collections.Counter'>
  • Now when you print the variable "e", you will see that the data in it is grouped already like below
>>> print(e)
Counter({'red': 3, 'green': 1, 'orange': 1, 'yellow': 1})
  • It is important to note that Counter are used as an alternative for dictionaries in data science due to the ease of counting data using them, like above.
  • You can fetch the most common data that the counter object holds like below. The below command fetches the two most common sets of data the counter object holds.
>>> e.most_common(2)
[('red', 3), ('green', 1)]
  • What if you want all the data without being paired like the {element: no. of occurrences} ? That is taken care of as well.
>>> list(e.elements())
['red', 'red', 'red', 'green', 'orange', 'yellow']

Note : In the above output, the order of elements entred is not retained, but the data has been grouped as red, green, orange and yellow.

  • Also, there might come a point when you want only the unique elements. That is covered as well.
>>> list(e)
['red', 'green', 'orange', 'yellow']
  • Maybe you might also want a list with just the occurance count of each element. Check below code snippet then.
>>> list(e.values())
[3, 1, 1, 1]
  • And if you want it like a dictionary with {key:value} pair, then here you go !
>>> list(e.items())
[('red', 3), ('green', 1), ('orange', 1), ('yellow', 1)]

Although everything has good and bad, I have not come across any bads of Counters. Would be more than happy if anyone could mention some.

Hope this helps.

Vallalarasu Pandiyan (Valla)

Quality Engineering Strategist | AI in Testing | COE | Sales | Presales | Test Automation Architect

7 年
回复
Prakash Ray

SRE & Performance Architect | Honeywell

7 年

Thanks. Akshay for the Article..

要查看或添加评论,请登录

Akshay Deshpande的更多文章

  • [Performance] : What does CPU% usage tell us ?

    [Performance] : What does CPU% usage tell us ?

    [Edit] -- All my latest articles are not published at - https://performanceengineeringin.wordpress.

    6 条评论
  • [Performance] : Understanding CPU Time

    [Performance] : Understanding CPU Time

    As a Performance Engineer, time and again you will come across a situation where you want to profile CPU of a system…

    4 条评论
  • Performance Engineering Checklists

    Performance Engineering Checklists

    Checklists; something that I truly believe in having for everything. Right from list of things to check before leaving…

    5 条评论
  • Random module in Python

    Random module in Python

    Time and again if you are building a small game or if you are trying to pick something randomly in Python, you will…

  • [Linux] Understanding Load Average

    [Linux] Understanding Load Average

    I am writing this quick article to help understand the importance of Load Average in Linux for a Performance Engineer…

    4 条评论
  • Understanding Physical and Logical CPUs

    Understanding Physical and Logical CPUs

    Here is a quick write up on what actually Physical and Logical CPU mean, and how are they different. To begin with, the…

    3 条评论
  • Jprofiler - CPU profiling

    Jprofiler - CPU profiling

    Jprofiler can serve many purposes, and CPU profiling and analysis is one of them. This article is more about how to…

    1 条评论
  • Learning how to learn [Python]

    Learning how to learn [Python]

    As Brain Herbert has rightly said: It is very much important to learn new things (anything) as learning never ends…

社区洞察

其他会员也浏览了