[英]Generate list of tuples (year, month, days_in_month, full_month) from list of dates
我有一个由以下生成的日期列表:
from dateutil import parser
from datetime import date, timedelta
d1 = parser.parse("2015-11-25")
d2 = parser.parse("2016-02-06")
delta = (d2-d1).days
date_list = [d1 + timedelta(days=x) for x in range(0, delta+1)]
在此列表中,2015 年 11 月有 6 天,2015 年 12 月有 31 天,2016 年 1 月有 31 天,2016 年 2 月有 6 天。2015 年 12 月和 2016 年 1 月是“完整”月份,即日期列表中的所有天数那几个月。
如何在 python 中以编程方式获取此信息,以生成如下列表:
[(2015,11,6,False),(2015,12,31,True),(2016,1,31,True),(2016,2,6,False)]
找到了一个简洁的解决方案:
from dateutil import parser
from datetime import date, timedelta
from collections import Counter
from calendar import monthrange
d1 = parser.parse("2015-11-25")
d2 = parser.parse("2016-02-06")
delta = (d2-d1).days
date_list = [d1 + timedelta(days=x) for x in range(0, delta+1)]
month_year_list = [(d.year, d.month) for d in date_list]
result = [(k[0],k[1],v , True if monthrange(k[0], k[1])[1] == v else
False) for k,v in Counter(month_year_list).iteritems()]
print result
遍历列表并累积每个年/月组合的天数:
import collections
days_in_year_month = defaultdict(int)
for each_date in date_list:
days_in_year_month[(each_date.year, each_date.month)] += 1
接下来输出元组,包括每年、每月、计数和 T/F:
import calendar
result = []
for year_month in date_list.keys():
days_in_ym = days_in_year_month([year_month[0], year_month[1])
is_complete = days_in_ym == calendar.monthrange(year_month[0], year_month[1])[1]
result.append(year_month[0], year_month[1], days_in_ym, is_complete)
所以:
前面提到的解决方案似乎还可以,但是我相信我有一个更优化的解决方案,因为它们需要计算一个包含所有天数的列表。 对于小的日期差异,这不会有问题。 但是,如果差异增加,您的列表将变得更大。
我想给出另一种更直观的方法,因为您基本上知道日期之间的所有月份都是满的,而日期本身的月份是不满的。
我尝试利用该信息,循环只会迭代日期之间的月数。
编码:
from dateutil import parser
from calendar import monthrange
d1 = parser.parse("2015-11-25")
d2 = parser.parse("2016-02-06")
# needed to calculate amount of months between the dates
m1 = d1.year * 12 + (d1.month- 1)
m2 = d2.year * 12 + (d2.month - 1)
result = []
# append first month since this will not be full
result.append((d1.year,d1.month,monthrange(d1.year, d1.month)[1]-d1.day+1,False))
current_month = d1.month
current_year = d1.year
# loop through the months and years that follow d1.
for _ in xrange(0,(m2-m1)-1):
if current_month+1 > 12:
current_month = 1
current_year += 1
else:
current_month += 1
result.append((current_year,current_month,monthrange(current_year, current_month)[1],True))
# append last month since this will not be full either.
result.append((d2.year,d2.month,d2.day,False))
print result
请记住,我给出的代码是一个示例,它不支持例如两个给定日期具有相同月份的场景。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.