1 #coding:utf-8 2 from collections import OrderedDict 3 import datetime 4 dt=['20171001','20171001','20171002','20171003','20171005','20171006','20171006','20171008','20171009','20171010','20171015'] 5 6 7 ''' 8 d1 = datetime.date(2015,10,7) 9 d2 = datetime.date(2015,8,15) 10 print((d1-d2).days) 11 12 ''' 13 14 def isnextday(dt1,dt2): 15 d1 = datetime.datetime.strptime(dt1, '%Y%m%d') 16 d2 = datetime.datetime.strptime(dt2, '%Y%m%d') 17 delta = (d2 - d1).days 18 if delta==1: 19 return 1 20 else: 21 return 0 22 23 d=OrderedDict() 24 l=[] 25 l2=[] 26 for i, val in enumerate(dt) : 27 d[i+1]=val 28 29 30 for k,v in d.items(): 31 if k==1: 32 l.append(1) 33 l2.append(1) 34 else: 35 if v==d.get(k-1): 36 l.append(0) 37 l2.append(0) 38 elif isnextday(d.get(k-1),v)==1: 39 l.append(0) 40 l2.append(1) 41 else: 42 l.append(1) 43 l2.append(1) 44 45 sum=0 46 for i in l: 47 sum+=i 48 49 sum2=0 50 for i in l2: 51 sum2+=i 52 53 54 print ("第一种 #连续出现的日期判定为1天 规则下统计 ") 55 print (str(sum)+'天') 56 57 print ('----------------------------------------') 58 59 print ("第二种 #一天出现多次判定为1天 规则下统计 ") 60 print (str(sum2)+'天')