课程: Hands-On Advanced Python: Data Engineering Basics
免费学习该课程!
今天就开通帐号,24,700 门业界名师课程任您挑!
Count values with defaultdict - Python教程
课程: Hands-On Advanced Python: Data Engineering Basics
Count values with defaultdict
- [Instructor] Earlier in the course, I used a dictionary to count how many years of weather data we had in the dataset. It's actually pretty fairly common to use dictionaries to keep track of counts and other data values and then map them back to keys. There's a data structure in Python's collections module that makes this process a little bit easier called the defaultdict, and I'm going to be using it in this example. So let's go ahead and open up the defaultdict source code file. And I covered the theory behind this collection class in my Advanced Python: Working With Data course. So if you want to learn a little more about the basics, be sure to check that out here in the library. So in order to use this class, I first have to import it from the collections module that is a part of the Python standard library. So from the collections, I'm going to import defaultdict. Oh, oops, one too many Ls. Here we go. Now…