- [Instructor] In the preceding video,
we showed you a way to use a dictionary
to summarize word counts.
Now, when you're doing things like that,
which are kind of common types of tasks,
it's often the case that Python already has a library class
that can help you with that task.
Summarizing word counts is a common technique
for text analysis and it turns out
that there is a type in the collections module,
which is part of the Python Standard Library
that can help us perform that task easily.
So far we've looked at a number
of different built-in collections.
We looked at lists, and tuples, and now dictionaries,
and soon, we're going to look at sets as well.
But there are other types of collections out there
and this particular module actually has a bunch
of additional collection types
that you can use in your Python code.
One of which is this Counter class.
And, it's a type of dictionary that knows how to,
as they say, count hashable objects.
Hashable objects are immutable objects
that you can use as the keys in a dictionary
or that you can use as the elements in a set,
where the set elements must be unique.
So anything hashable, which would be things
like strings and the numeric values,
like integers and floating point numbers,
typically they're going to be immutable types of objects.
Most commonly, they're going to be strings.
But we can take advantage of this built-in Counter class
in the collections module to perform the work
that we showed you in the preceding script, in that loop.
So it's going to do all of that work for us.
Now, to demonstrate this concept,
let's switch over to the terminal window here.
And, let's go ahead and from that collections module,
let's import the Counter class.
And, just to demonstrate, by the way,
we're going to take a string,
paste it in the same text statement
that we created in the script.
So we're going to once again have this string,
well actually two strings,
that are going to be assembled together into one string.
And, remember what we did in the script is,
we told that string to split itself
into a list of individual strings.
So just to show you what that does,
if I say text.split, you get a list.
There's the square brackets
denoting a list of string values,
comment limited of course,
and these are all the individual words
that you see in the text body up above here.
So what we're going to do is
feed text.split into the creation of a Counter object
and as part of creating the object,
it's automatically going to summarize
all of these words into their word counts.
So to do that, we're going to create an object,
I'll call counter, with a lower case c to differentiate it,
from the class name.
And, we'll say text.split here,
and that's going to go create the Counter object.
And, to save me a tiny bit of typing time here,
I'm going to go ahead and put in a little for loop,
that's going to iterate
through my new Counter object's items.
So it is still a dictionary
and therefore I can still call the items method on it.
And, the items method, of course,
is going to give me back key value pairs as tuples.
I'm going to take those tuples and pass them off
to the sorted built-in function,
which will give me those tuples in lexicographical order.
These are all lower-case strings
so they will be in alphabetical order.
And, for each word and corresponding count,
we're going to display a line of text
containing the word and the count.
So let's go ahead and execute that.
There are all of the words and all of the counts,
and if you were to go back and compare them
to the output of the preceding script,
where we used the same text,
you'll see we got the same exact results.
Now, separately we can also go ahead,
like we did in the preceding script,
and display the number of unique keys once again.
So to do that we're going to use the length
of the counter.keys method call.
So counter.keys is going to give us the actual collection
of keys which is a set of unique values,
and the length of that, of course,
is the number of unique keys.
We can also simply take the length
of the Counter object itself
because it is a dictionary
and the number of key value pairs
would also be the number of unique keys.
So as you can see once again,
the number of unique keys is 10.