按年、月或日筛选日期列表



如何按特定年份、月份或日期筛选datetime.date对象列表?

示例:

import datetime as dt
dates = [dt.date(2017, 5, 6), dt.date(2015, 5, 13), dt.date(2012, 12, 6), dt.date(2017, 12, 6)]
import datetime as dt
dates = [dt.date(2017, 5, 6), dt.date(2015, 5, 13), dt.date(2012, 12, 6), dt.date(2017, 12, 6)]
# Filter by year
dates_2017 = list(filter(lambda x: x.year == 2017, dates))
# Filter by month
dates_may = list(filter(lambda x: x.month == 5, dates))
# Filter by day
dates_6th = list(filter(lambda x: x.day == 6, dates))

最新更新