Counting Without Counter
00:00
In the previous lesson, I gave an overview of the course. In this lesson, I’ll introduce you to solving counting problems without Python’s Counter
class, doing it the hard way first. My mother would be proud.
00:12 Computing is full of counting problems. One of the first things you learn to do with a sequence, such as a list, is to find its length. That’s a basic counting problem.
00:22 How many things are there in that sequence? Finding the length of a sequence is only at the beginning, though. What about grouping things together in your data? Counting these kinds of things is called determining the frequency of an occurrence.
00:35
Let’s go to the REPL, and I’ll show you what I’m talking about. Let me start without using any libraries, just some straight Python data types. Take the word "mississippi"
.
00:49 What if you wanted to count the occurrences of each of the letters in the word? You could create a dictionary, then for each letter in the word, check if that letter is already in your dictionary.
01:08
If it isn’t, then create and initialize the key-value pair to 0
,
01:16
and then either way, increment the counting value. Looking at the result in the dictionary, you’ll see a key for each unique letter and the corresponding count of the letter as the value: one 'm'
, four 'i'
s, four 's'
s, and two 'p'
s.
01:32 By the time you’re done, you’re going to be tired of Mississippi, I promise. Initializing a new key in the dictionary can be done in other ways. Let’s skin the proverbial cat—what a horrid expression!
01:48
Same initial dict
, same for
loop …
01:59
and this time, instead of a conditional to initialize the value and a line increment, it can be done in a single statement. The .get()
method of a dictionary takes a default value parameter. So in this case, either the letter is fetched and then incremented or 0
is returned and incremented.
02:17
The new value is set into the dictionary at the corresponding key. The counter
dict
now has the same result as before. And you could also do it with the defaultdict
class.
02:37
defaultdict
is a specialty dictionary class that lives in the collections
module.
02:45
And this time, instead of creating an empty dict
, you create a new defaultdict
class, passing in int
. The hint here is in the class’s name. It is a dictionary with a default value. Passing in int
means that if a key isn’t found, create it, setting it to an integer.
03:02
The default value for an integer is 0
, so our new key will be set to 0
. As before, same loop.
03:15
And this time a much easier-to-read line. This is doing the same thing as .get()
before, but by using the defaultdict
, the default key behavior sets it to 0
, and then you can increment it.
03:29
Looking at the counter, you see that it looks a little different, as the result isn’t a plain old dictionary, but a default one. The counts inside are the same, and as defaultdict
inherits from dict
, anything you can do with the base class you can do with this fancier one. Okay.
03:46
I think you’ve got a taste for the problem. As this course is titled Counting With Python’s Counter
, it shouldn’t be a surprise that the next lesson is on using the Counter
class.
Become a Member to join the conversation.