[英]Filter list of dates by year, month or day
如何按特定年、月或日过滤 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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.