Python日历类| 带有示例的monthdays2calendar()方法

Python Calendar.monthdays2calendar()方法 (Python Calendar.monthdays2calendar() Method)

Calendar.monthdays2calendar() method is an inbuilt method of the Calendar class of calendar module in Python. It uses an instance of this class and returns a list of the weeks in the given month as full weeks. Weeks, given here are lists of seven tuples, where each tuple consists of the day number and the weekday number of that day. Since weeks are written as full weeks, days outside the month are represented as 0.

Calendar.monthdays2calendar()方法是Python中Calendar模块的Calendar类的内置方法。 它使用此类的实例,并返回给定月份中整周的列表。 这里给出的星期是七个元组的列表,其中每个元组都由日期和星期几组成。 由于周被写为整周,因此月外的天表示为0。

Module:

模块:

    import calendar

Class:

类:

    from calendar import Calendar

Syntax:

句法:

    monthdays2calendar(year, month)

Parameter(s):

参数:

  • year: It is a required parameter, which specifies the year of the calendar.

    year :这是必填参数,用于指定日历的年份。

  • month: It is a required parameter, which specifies the month of the calendar.

    month :这是必填参数,用于指定日历的月份。

Return value:

返回值:

The return type of this method is <class 'list'>, it returns a list of the weeks in the given month, where each tuple represents the day and date on that date.

此方法的返回类型为<class'list'> ,它返回给定月份中星期的列表,其中每个元组代表该日期的日期和日期。

Example:

例:

# Python program to illustrate the 
# use of monthdays2calendar() method

# import class
import calendar

# Creating Calendar Instance
cal = calendar.Calendar()
year = 2018
month = 11

# Here first value is the day of the month
# and second value is the weekday number 
# where Monday is 0 till Sunday which is 6
print("Days outside of the month are 0")
print("Weekwise calendar of November 2018 with first weekday as Monday")
print(cal.monthdays2calendar(year, month))
print()
# Note tuples always start from firstweekday value
# Full weeks are listed.

# set the firstweekday to 1
cal = calendar.Calendar(firstweekday = 5)
year = 1994
month = 4

print("Weekwise calendar of November 2011 with first weekday as Saturday")
print(cal.monthdays2calendar(year, month))
print()

Output

输出量

Days outside of the month are 0
Weekwise calendar of November 2018 with first weekday as Monday
[[(0, 0), (0, 1), (0, 2), (1, 3), (2, 4), (3, 5), (4, 6)], [(5, 0), (6, 1), (7, 2), (8, 3), (9, 4), (10, 5), (11, 6)], [(12, 0), (13, 1), (14, 2), (15, 3), (16, 4), (17, 5), (18, 6)], [(19, 0), (20, 1), (21, 2), (22, 3), (23, 4), (24, 5), (25, 6)], [(26, 0), (27, 1), (28, 2), (29, 3), (30, 4), (0, 5), (0, 6)]]

Weekwise calendar of November 2011 with first weekday as Saturday
[[(0, 5), (0, 6), (0, 0), (0, 1), (0, 2), (0, 3), (1, 4)], [(2, 5), (3, 6), (4, 0), (5, 1), (6, 2), (7, 3), (8, 4)], [(9, 5), (10, 6), (11, 0), (12, 1), (13, 2), (14, 3), (15, 4)], [(16, 5), (17, 6), (18, 0), (19, 1), (20, 2), (21, 3), (22, 4)], [(23, 5), (24, 6), (25, 0), (26, 1), (27, 2), (28, 3), (29, 4)], [(30, 5), (0, 6), (0, 0), (0, 1), (0, 2), (0, 3), (0, 4)]]


翻译自: https://www.includehelp.com/python/calendar-calendar-monthdays2calendar-method-with-example.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值