Change language

Counting the number of times items appear with collections.Counter and Python – A Python short

Counting the number of times items appear with collections.Counter and Python - A Python short

Hey there, Michael here with another Python Short where Im going to teach you something amazing with Python in just a few minutes in this one, were going to count the occurrence or the number of times a word or item appears in a collection or list youve traveled to a conference, and you asked this group of people. Hey everyone what country are you from where do you travel here from. Some. people might say USA. Another person says France another person says Mexico and in your list you might have Mexico Mexico USA France Mexico how many people actually came from each country which one is the most common country turns out.

Python lets you answer that in two lines of code its gonna be amazing lets get to it Lets imagine watching a formula one race they have this concept of the driver of the day this is like an audience participation thing you get to vote during the race to say which driver has been the most outstanding that day who has overcome the most adversity or past the most people or whatever it is that you care the most about. Youll see a graphic like this driver of the day, in this case Lewis Hamilton 39% Max Verstappen 21% Romain Grosjean 18% so these are the drivers of the day and you know that list. Its just like I talked about the countries its Lewis Lewis Max Roman Lewis Max other person and so on. So what we want to do is use Python given that list to generate, basically this output right here, not the graphic of it, but the numbers Lewis got 39% ax got such and such percent and so on. Here we are in PyCharm and you can see we have that list of those names and if it wasnt a vote for one of the top three I just put other, so thats going to appear where its going to ignore it, so how are we going to work with this data. You can see therere tons and tons of that sure we could write a loop and we could count it up. Thats not the Python way now is it were going to go over here, were going to say from collections import this amazing thing called counter and thats pretty much all were going to need to do, so what well do is well go down here and well just say c = counter and we give it this list remember this is the list that has just the individual items in it and what do we do after we do this? well we can print out the answer. Actually theres all sorts of things here and the one were looking for is most common, so lets go and run this look at that Lewis Hamilton got 100 votes and Ignore other, Max got 53 Roman Grojeon got 48. How? incredibly simple is that again, knowing the right library or module or data structure in Python really really solves this problem so much better than trying to figure out ourselves but you know we could write something better than that watch this. So lets go over here and were, going to get the most common, so well say for you let me print it out one more time for you notice what we get here is the value the name and then the count, the value, and the account, the value in the account so what we can do is we can do tuple unpacking and we can say, for name, count in c .most common all right and thats gonna give us Max his count Lewis and his count and so on and then we can go over here and just print out something like this, lets say the name and instead of putting just, well, lets just put the count for a minute so you can just see where well go and well build this up here so if we run that notice Lewis key other got 70 were going to exclude them and so on so perfect were getting those projected into those two values lets put it like this. Lets say if the name is other. Were gonna not do more in the loop that should give us the ones that we want perfect but we want a percent right thats what our graphic was. It was 38% not some random number so we can say right here percent is going to be a count divided by well how many there were how do we know we go to this counter we say total like this, then we can print out the percent thats sort of the percent thats the ratio its also a huge long thing there but notice its lining up pretty close with our graph that we had earlier, so we want to make it truly a percent so well do like these times 100 and then convert it to an integer, all right and thats going to be closer. Yeah thats actually what we want, we just want the percent sign there at the end, all right percent there it is Lewis Hamilton in this case with this set of votes is 36% by 36 percent of the votes the winner of driver of the day Max Verstappen got 19% and roman got 17% super super neat. We got this long list we import counter from collections. We work with most common we get a tuple of the value and the count that value got boom were done. So very very cool way to work with sets of data and pile it up into a report collections.counter learn it, love it, use it There was, you now know, way more about counting items and using some of these built-in modules like collections.counter if you enjoy learning something quick with Python like this. Be sure to subscribe to my channel, just hover over my face there and hit subscribe and if you enjoyed this video do like it it really helps support our work also check out the talk python to me podcast and talk python training where we have hundreds of hours of courses on Python talking about these types of topics and going way deeper. Thanks for watching the video catch you later.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically