Input: Year as an int.
Output: The list of most frequent days sorted by the order of days in week (from Monday to Sunday).
找出一年中最多的那个星期几。
from datetime import *
from datetime import timedelta
def most_frequent_days(year):
a={"Monday":0,"Tuesday":0,"Wednesday":0,"Thursday":0,"Friday":0 ,"Saturday":0,"Sunday":0}
e={"Monday":0,"Tuesday":1,"Wednesday":2,"Thursday":3,"Friday":4 ,"Saturday":5,"Sunday":6}
b={0:"Monday",1:"Tuesday",2:"Wednesday",3:"Thursday",4:"Friday",5:"Saturday",6:"Sunday"}
now = date(year,1,1)
while(now.year==year):
a[b[now.weekday()]]+=1
now = now + timedelta(days=1)
c=sorted(a.items(),key=lambda asd:asd[1],reverse =True)
d=[]
while(c[-1][1] != c[0][1]):
c.pop()
for i in range(len(c)):
d.append(c[i][0])
return sorted(d,key=lambda x:e[x])
这个好。后来想到了,只要看第一周和最后一周就可以了,有相交的weekday就取相交的那几天。没有就取或。callendar模块这里写链接内容
from datetime import datetime
from calendar import day_name
def most_frequent_days(year):
first = set(range(datetime(year, 1, 1).weekday(), 7))
last = set(range(datetime(year, 12, 31).isoweekday()))
return [day_name[day] for day in sorted((first & last) or (first | last))]