python - 使用 Python 中的日历模块确定有 5 个或更多星期日的月份?
问题描述
例如,我需要编写一个代码来查找 2018 年的哪个月有 5 个或更多星期日?我想在 Python 中使用日历模块并以缩写形式保存生成的月份。我看到了日历模块的文档,但无法弄清楚代码。
解决方案
这是一种方法。此外,返回https://docs.python.org/3/library/calendar.html并尝试使用其中描述的一些内容。
import calendar
def find_five_sunday_months(year):
calendar.setfirstweekday(calendar.SUNDAY)
five_sunday_months = []
for month in range(1, 13):
calendar_month = calendar.monthcalendar(year, month)
# If you're counting Sunday as the first day of the week, then any month that extends into
# six weeks, or starts on a Sunday and extends into five weeks, will contain five Sundays.
if len(calendar_month) == 6 or (len(calendar_month) == 5 and calendar_month[0][0] == 1):
five_sunday_months.append(calendar.month_abbr[month])
return five_sunday_months
print (find_five_sunday_months(2018))
推荐阅读
- sql - 如何在 SQL 中将列合并为一列?
- javascript - 如何使用 jQuery 滚动(元素底部应位于页面底部)
- python - 熊猫中的时间增量矩阵(成对)
- python - 如何防止循环重复我的随机值?
- sql - SQL Server中如何通过行比较计算一致性程度?
- entity-framework - Asp.net MVC 实体框架路径不起作用 - WebAPI 控制器类
- postgresql - 在 Postgres 或其他关系数据库服务器中将插入包装到事务中有什么好处?
- java - 如何从事件处理程序向 Java FX 主线程发出信号?
- laravel - 哪里没工作,在有很多关系
- node.js - NestJS 将下载的文件直接传递给客户端