课程: Advanced Python (2018)

今天就学习课程吧!

今天就开通帐号,24,600 门业界名师课程任您挑!

defaultdict

defaultdict

- [Instructor] Just about every non-trivial Python program uses dictionaries in some way. They are a great way to associate data values with unique keys for fast lookup. The collections module provides two interesting dictionary subclasses to help out with common scenarios where a regular dictionary would need unnecessary code. So, one such example is the default dict, which we'll examine in this example. It's a fairly common scenario to use dictionaries to keep track of data, such as the result of counting operations. So, here in VS Code, I'm going to open up the defaultdict_start example in my collections folder, and, you can see that I have a list here of different kinds of fruit, and I want to count the number of each kind of fruit that I have. So, I have some code that iterates over the list, and uses a dictionary to keep track of the count of each type of fruit. Now, the code that I have right now is going to produce an error, when I try to run it, so let's see. I'll try to run…

内容