按公历月份统计事件数量
佛罗里达州全年都有可能遭遇飓风登陆。正如我们之前讨论的,某些月份更容易发生飓风。
使用 florida_hurricane_dates,来看一看佛罗里达的飓风在一年各个月份的分布情况。
我们已经创建了一个名为 hurricanes_each_month 的字典来保存计数,并将初始值设为 0。您需要遍历飓风日期列表,在循环中为对应月份的 hurricanes_each_month 计数加一,然后打印结果。
本练习是课程的一部分
在 Python 中处理日期和时间
练习说明
- 在
for循环内部: - 将
month设为该次飓风的月份。 - 将对应月份的
hurricanes_each_month计数加 1。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# A dictionary to count hurricanes per calendar month
hurricanes_each_month = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6:0,
7: 0, 8:0, 9:0, 10:0, 11:0, 12:0}
# Loop over all hurricanes
for hurricane in florida_hurricane_dates:
# Pull out the month
month = hurricane.____
# Increment the count in your dictionary by one
hurricanes_each_month[month] ____
print(hurricanes_each_month)