Exploring Practical Applications: Part 2
In the previous lesson, I showed you how to count letter frequency in a file and how to build an ASCII histogram. In this lesson, I’ll continue showing you some more practical applications of the
It creates a counter based on the data passed in. Line 6 gets the count of the most frequent item. Remember that
.most_common() returns a list of tuples, so this throws away the most frequent items name using the underscore (
_) convention and stores just the corresponding frequency value in
top_count. Lines 9 through 11 then loop through the data, finding all the items whose frequency match
02:59 I’ve seen too much code where it isn’t clear what the overloaded operator’s supposed to do, but here, paths use slash as separators, and it’s easy to ignore the fact that this is actually overriding division of all things. I’m on a Mac, which is a Unix-based operating system.
If you’re on Windows and coding along, you’ll have to modify this path to give you something that makes sense in your environment. For me, this joins my
.home directory path with the
'Downloads' directory underneath it.
.iterdir() method on a
Path object returns an iterator of all the files and directories underneath the path. If the path this
Path object pointed to wasn’t valid, this is where it would fall over.
04:15 Here I’ve built a list comprehension that is listing through all of the entries. Those are the files and directories. For each entry found, it checks if it is a file. If it is, it will be used in the comprehension. This way, child directories don’t end up in the list.
Instead of storing the entry itself, I’m using another feature of the
Path object, the
.suffix property. This is the extension part of the filename. So to recap,
extensions ends up being a list that contains the extensions of each of the filenames found in any directory below the
Of course, you can use
.most_common() to get at the most frequent items. Quick thing to note: if you look more closely at the counter, you’ll see that it isn’t case-sensitive. There is one
You could fix this by changing the list comprehension to call
.lower() on the suffix. A natural use of a Python counter is a shopping cart. The keys are the items being put in the cart, and the values are the number of the items being purchased. Let’s try this out in the REPL.
06:34 I’ve created a counter with the things I’m buying. One copy of the course, three copies of a book, and some wallpaper. I’m assuming it’s digital wallpaper. Otherwise this is a bit of a weird store.
And that long f-string prints it all out. The
7.2fs you see inside of the f-string are format specifiers indicating that the price and subtotals should be seven characters wide with two decimal places shown.
And there you have it, the receipt for your cart. For a little homework, add the lines you’d need to print out a total as well. Next up, I’ll show you how to use the
Counter class as a multiset implementation.
Become a Member to join the conversation.