From the course: Advanced Python: Working With Data

Unlock the full course today

Join today to access over 23,400 courses taught by industry experts.

Solution:  Classify event types

Solution: Classify event types - Python Tutorial

From the course: Advanced Python: Working With Data

Solution: Classify event types

(upbeat music) - [Instructor] For my solution, I decided to use the defaultdict collection class. And I made this decision for two reasons. First, I wanted to count the number of each event and second, I wanted an easy way to associate the event type with the count. The dictionary class gives us an easy way to do that, and the defaultdict class makes it even easier by declaring an object that uses integers as the default value type. So my code starts by declaring a new defaultdict, and I tell it that I'm going to use integers as the value types, and then I just process each event in the earthquake data in a loop. And I use the event type as the key into the dictionary and I just increase the count for each type as I come across each type of event. And then when I'm done with the list, I just print out each key and value in the dictionary along with a little formatting to make it look nice. All right, so that's my solution.…

Contents