不用滚动自己的
consecutive函数,您可以简单地使用
.toordinal()datetime对象的方法将日期对象转换为整数。序数日期集的最大值和最小值之差比该长度的长度大一个:
from datetime import datetimedate_strs = ['07-06-2010', '06-24-2010', '07-05-2010', '06-25-2010']# date_strs = ['02-29-2012', '02-28-2012', '03-01-2012']# date_strs = ['01-01-2000']dates = [datetime.strptime(d, "%m-%d-%Y") for d in date_strs]date_ints = set([d.toordinal() for d in dates])if len(date_ints) == 1: print "unique"elif max(date_ints) - min(date_ints) == len(date_ints) - 1: print "consecutive"else: print "not consecutive"
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)