Exploring Practical Applications: Part 1
In the previous lesson, I introduced you to the
Counter class. In this lesson, I’ll show you a few practical uses of this class. Have you ever wondered how frequently a letter occurs in a block of text? Well, wonder no more. In this first practical application, I’ll show you how to use the
Counter class to count the letters in a file.
This function finds the frequency of letters in a text file. Line 5 creates a counter for tracking the letters. Line 6 opens the file using the
filename argument for the function. Lines 7 through 11 is what counts the actual letters.
for block inside of the comprehension tells you what is going to be inside of
char. It’s going to iterate over the results of
line is a string containing the line from the text file, and
lower() is a function that returns an all-lowercase version of the string.
Putting that all together, the list will contain all the letters in a line after they’ve been transformed to lowercase, and only if they’re an alphabetic character. This way, punctuation and numbers won’t be included in the comprehension. Finally, line 11 is where the counting happens. The
letter_counter object is updated.
and here’s the result. Not surprisingly to any Scrabble players out there, the letter
e is the most common occurrence … and not a single ten-point
q in the whole thing. In case you wanted only the five most popular …
03:44 Remember that this returns a list of tuples ordered by frequency. The rest of the code is presentation. Line 6 is a dictionary comprehension. This dictionary will contain key-value pairs mapping items to be graphed with the number of symbols in the graph, the ASCII bar in the bar chart.
04:11 Multiplication of a string in Python gives you repetition. Note that the order of the items in the dictionary will be the order of the frequency of things being counted. As of Python 3.7, dictionaries are ordered dicts by default.
They were implemented that way in CPython earlier than that, but as of Python 3.7, it is part of the language spec. If you’re running something other than CPython or a version before 3.5, you would need to implement this with an
OrderedDict class instead.
You should also probably look into upgrading if you can. 3.6 has gone end-of-life. Line 7 finds the longest label in the graph so that all the labels can be padded to this length. Lines 8 through 10 loop through the
chart dictionary, printing a padded version of the label and the resulting bar graph. Let’s see this in practice.
Once again, a nice little ASCII histogram. For a little homework, see if you can add the ability to scale the graph. Add a
max_width argument to the function and scale the bars so that they’re never longer than that maximum.
Become a Member to join the conversation.