This exercise is Part 3 of 4 of the birthday data exercise series. The other exercises are: Part 1, Part 2, and Part 4.
In the previous exercise we saved information about famous scientists’ names and birthdays to disk. In this exercise, load that JSON file from disk, extract the months of all the birthdays, and count how many scientists have a birthday in each month.
Your program should output something like:
You already have the skills to achieve this exercise with concepts we’ve already covered: for loops, dictionaries, and basic arithmetic. However, I want to talk about a Python built-in called a Counter
.
A Counter
takes a list and counts how many of each element were in the list. To use the Counter, first import it from collections
:
This lets you use the Counter
data structure built into Python in your program. Then, give it a list:
If you print(c)
, you will see this:
This means there are 3 ham
, 2 roast beef
, and 2 cheese
sandwiches in my list. I can get this information directly from the Counter
:
Hope this is useful!