簡體   English   中英

按年、月或日過濾日期列表

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM