【发布时间】:2016-11-10 02:51:14
【问题描述】:
我正在尝试计算给定月份内特定日期的频率。例如,本月(2016 年 11 月)有 4 个星期一、5 个星期二、5 个星期三、4 个星期四、4 个星期五、4 个星期六和 4 个星期日。
到目前为止,这是我已经完成的。
import calendar
from calendar import weekday, monthrange, SUNDAY
import datetime
now = datetime.datetime.now()
year, month = now.year, now.month
days = [weekday(year, month, d) for d in range(*monthrange(year, month))]
但是,当我尝试打印本月内有多少个(例如,星期二)时,结果不正确。
In [1]: print(days.count(calendar.WEDNESDAY))
Out [1]: 4 # should be 5 instead of 4
In [2]: print(days.count(calendar.TUESDAY))
Out [2]: 5 # this one is correct
如果我检查 Python 本身的日历,它会显示正确的日历。
In [4]: calendar.prmonth(year, month)
November 2016
Mo Tu We Th Fr Sa Su
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
我的目标是计算给定月份内特定日期的频率。任何建议将不胜感激。非常感谢。
问候,
阿诺德 A.
【问题讨论】:
标签: python python-3.x calendar