按曆法月份統計事件數
佛羅里達州一年四季都有可能遇到颶風登陸。如同我們先前提到的,有些月份比其他月份更容易發生颶風。
使用 florida_hurricane_dates,來看看佛羅里達的颶風在一年當中各月份的分布情況。
我們已建立一個名為 hurricanes_each_month 的字典來儲存你的計數,並把初始值都設為 0。你將迴圈走訪這個颶風日期清單,在過程中把對應月份於 hurricanes_each_month 的值加 1,最後列印結果。
本練習屬於課程
在 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)