Python日历类| yeardays2calendar()方法与示例

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

Calendar.yeardays2calendar() method is an inbuilt method of the Calendar class of calendar module in Python. It uses an instance of this class and returns the list of the month rows for the given year, representing dates as tuples. Each month has full weeks, ie, each week has all the seven days and if suppose someday in that week is outside of that month, its date value is 0.

Calendar.yeardays2calendar()方法是Python中Calendar模块的Calendar类的内置方法。 它使用此类的实例,并返回给定年份的月行列表,将日期表示为元组。 每个月都有完整的星期,即每个星期都有所有的7天,并且假设该周中的某天不在该月中,则其日期值为0。

Each value in a week list is a tuple consisting of day numbers and weekday numbers.

周列表中的每个值都是一个由日期数字和工作日数字组成的元组。

Module:

模块:

    import calendar

Class:

类:

    from calendar import Calendar

Syntax:

句法:

    yeardays2calendar(year, width =3)

Parameter(s):

参数:

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

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

  • width: It is an optional parameter, which specifies the width of the month value; default value = 3.

    width :这是一个可选参数,用于指定月份值的宽度; 默认值= 3。

Return value:

返回值:

The return type of this method is <class 'list'>, it returns a list of month rows.

此方法的返回类型为<class'list'> ,它返回月份行列表。

Example:

例:

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

# import class
import calendar

# Creating Calendar Instance
cal = calendar.Calendar()
year = 2013
# default width =3
# 1 week contains 7 tuples
print(cal.yeardays2calendar(year))
print()
# first value of tuple is day number of the month
# second value is weekday number of the day
# days outside the month are 0

Output

输出量

[[[[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)], [(7, 0), (8, 1), (9, 2), (10, 3), (11, 4), (12, 5), (13, 6)], [(14, 0), (15, 1), (16, 2), (17, 3), (18, 4), (19, 5), (20, 6)], [(21, 0), (22, 1), (23, 2), (24, 3), (25, 4), (26, 5), (27, 6)], [(28, 0), (29, 1), (30, 2), (31, 3), (0, 4), (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), (0, 4), (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), (31, 6)]]], [[[(1, 0), (2, 1), (3, 2), (4, 3), (5, 4), (6, 5), (7, 6)], [(8, 0), (9, 1), (10, 2), (11, 3), (12, 4), (13, 5), (14, 6)], [(15, 0), (16, 1), (17, 2), (18, 3), (19, 4), (20, 5), (21, 6)], [(22, 0), (23, 1), (24, 2), (25, 3), (26, 4), (27, 5), (28, 6)], [(29, 0), (30, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6)]], [[(0, 0), (0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)], [(6, 0), (7, 1), (8, 2), (9, 3), (10, 4), (11, 5), (12, 6)], [(13, 0), (14, 1), (15, 2), (16, 3), (17, 4), (18, 5), (19, 6)], [(20, 0), (21, 1), (22, 2), (23, 3), (24, 4), (25, 5), (26, 6)], [(27, 0), (28, 1), (29, 2), (30, 3), (31, 4), (0, 5), (0, 6)]], [[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 5), (2, 6)], [(3, 0), (4, 1), (5, 2), (6, 3), (7, 4), (8, 5), (9, 6)], [(10, 0), (11, 1), (12, 2), (13, 3), (14, 4), (15, 5), (16, 6)], [(17, 0), (18, 1), (19, 2), (20, 3), (21, 4), (22, 5), (23, 6)], [(24, 0), (25, 1), (26, 2), (27, 3), (28, 4), (29, 5), (30, 6)]]], [[[(1, 0), (2, 1), (3, 2), (4, 3), (5, 4), (6, 5), (7, 6)], [(8, 0), (9, 1), (10, 2), (11, 3), (12, 4), (13, 5), (14, 6)], [(15, 0), (16, 1), (17, 2), (18, 3), (19, 4), (20, 5), (21, 6)], [(22, 0), (23, 1), (24, 2), (25, 3), (26, 4), (27, 5), (28, 6)], [(29, 0), (30, 1), (31, 2), (0, 3), (0, 4), (0, 5), (0, 6)]], [[(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), (31, 5), (0, 6)]], [[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (1, 6)], [(2, 0), (3, 1), (4, 2), (5, 3), (6, 4), (7, 5), (8, 6)], [(9, 0), (10, 1), (11, 2), (12, 3), (13, 4), (14, 5), (15, 6)], [(16, 0), (17, 1), (18, 2), (19, 3), (20, 4), (21, 5), (22, 6)], [(23, 0), (24, 1), (25, 2), (26, 3), (27, 4), (28, 5), (29, 6)], [(30, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6)]]], [[[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)], [(7, 0), (8, 1), (9, 2), (10, 3), (11, 4), (12, 5), (13, 6)], [(14, 0), (15, 1), (16, 2), (17, 3), (18, 4), (19, 5), (20, 6)], [(21, 0), (22, 1), (23, 2), (24, 3), (25, 4), (26, 5), (27, 6)], [(28, 0), (29, 1), (30, 2), (31, 3), (0, 4), (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), (0, 5), (1, 6)], [(2, 0), (3, 1), (4, 2), (5, 3), (6, 4), (7, 5), (8, 6)], [(9, 0), (10, 1), (11, 2), (12, 3), (13, 4), (14, 5), (15, 6)], [(16, 0), (17, 1), (18, 2), (19, 3), (20, 4), (21, 5), (22, 6)], [(23, 0), (24, 1), (25, 2), (26, 3), (27, 4), (28, 5), (29, 6)], [(30, 0), (31, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6)]]]]

Example:

例:

# Python program to illustrate the 
# use of yeardays2calendar() method with changing width

# import class
import calendar

# Creating Calendar Instance
cal = calendar.Calendar()
year = 1997
# 1 week contains 7 tuples

for i in cal.yeardays2calendar(year, width = 5):
    print(i)
print()
# first value of tuple is day number of the month
# second value is weekday number of the day
# days outside the month are 0

Output

输出量

[[[(0, 0), (0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)], [(6, 0), (7, 1), (8, 2), (9, 3), (10, 4), (11, 5), (12, 6)], [(13, 0), (14, 1), (15, 2), (16, 3), (17, 4), (18, 5), (19, 6)], [(20, 0), (21, 1), (22, 2), (23, 3), (24, 4), (25, 5), (26, 6)], [(27, 0), (28, 1), (29, 2), (30, 3), (31, 4), (0, 5), (0, 6)]], [[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 5), (2, 6)], [(3, 0), (4, 1), (5, 2), (6, 3), (7, 4), (8, 5), (9, 6)], [(10, 0), (11, 1), (12, 2), (13, 3), (14, 4), (15, 5), (16, 6)], [(17, 0), (18, 1), (19, 2), (20, 3), (21, 4), (22, 5), (23, 6)], [(24, 0), (25, 1), (26, 2), (27, 3), (28, 4), (0, 5), (0, 6)]], [[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 5), (2, 6)], [(3, 0), (4, 1), (5, 2), (6, 3), (7, 4), (8, 5), (9, 6)], [(10, 0), (11, 1), (12, 2), (13, 3), (14, 4), (15, 5), (16, 6)], [(17, 0), (18, 1), (19, 2), (20, 3), (21, 4), (22, 5), (23, 6)], [(24, 0), (25, 1), (26, 2), (27, 3), (28, 4), (29, 5), (30, 6)], [(31, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6)]], [[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)], [(7, 0), (8, 1), (9, 2), (10, 3), (11, 4), (12, 5), (13, 6)], [(14, 0), (15, 1), (16, 2), (17, 3), (18, 4), (19, 5), (20, 6)], [(21, 0), (22, 1), (23, 2), (24, 3), (25, 4), (26, 5), (27, 6)], [(28, 0), (29, 1), (30, 2), (0, 3), (0, 4), (0, 5), (0, 6)]], [[(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), (31, 5), (0, 6)]]]
[[[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (1, 6)], [(2, 0), (3, 1), (4, 2), (5, 3), (6, 4), (7, 5), (8, 6)], [(9, 0), (10, 1), (11, 2), (12, 3), (13, 4), (14, 5), (15, 6)], [(16, 0), (17, 1), (18, 2), (19, 3), (20, 4), (21, 5), (22, 6)], [(23, 0), (24, 1), (25, 2), (26, 3), (27, 4), (28, 5), (29, 6)], [(30, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6)]], [[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)], [(7, 0), (8, 1), (9, 2), (10, 3), (11, 4), (12, 5), (13, 6)], [(14, 0), (15, 1), (16, 2), (17, 3), (18, 4), (19, 5), (20, 6)], [(21, 0), (22, 1), (23, 2), (24, 3), (25, 4), (26, 5), (27, 6)], [(28, 0), (29, 1), (30, 2), (31, 3), (0, 4), (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), (31, 6)]], [[(1, 0), (2, 1), (3, 2), (4, 3), (5, 4), (6, 5), (7, 6)], [(8, 0), (9, 1), (10, 2), (11, 3), (12, 4), (13, 5), (14, 6)], [(15, 0), (16, 1), (17, 2), (18, 3), (19, 4), (20, 5), (21, 6)], [(22, 0), (23, 1), (24, 2), (25, 3), (26, 4), (27, 5), (28, 6)], [(29, 0), (30, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6)]], [[(0, 0), (0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6)], [(6, 0), (7, 1), (8, 2), (9, 3), (10, 4), (11, 5), (12, 6)], [(13, 0), (14, 1), (15, 2), (16, 3), (17, 4), (18, 5), (19, 6)], [(20, 0), (21, 1), (22, 2), (23, 3), (24, 4), (25, 5), (26, 6)], [(27, 0), (28, 1), (29, 2), (30, 3), (31, 4), (0, 5), (0, 6)]]]
[[[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 5), (2, 6)], [(3, 0), (4, 1), (5, 2), (6, 3), (7, 4), (8, 5), (9, 6)], [(10, 0), (11, 1), (12, 2), (13, 3), (14, 4), (15, 5), (16, 6)], [(17, 0), (18, 1), (19, 2), (20, 3), (21, 4), (22, 5), (23, 6)], [(24, 0), (25, 1), (26, 2), (27, 3), (28, 4), (29, 5), (30, 6)]], [[(1, 0), (2, 1), (3, 2), (4, 3), (5, 4), (6, 5), (7, 6)], [(8, 0), (9, 1), (10, 2), (11, 3), (12, 4), (13, 5), (14, 6)], [(15, 0), (16, 1), (17, 2), (18, 3), (19, 4), (20, 5), (21, 6)], [(22, 0), (23, 1), (24, 2), (25, 3), (26, 4), (27, 5), (28, 6)], [(29, 0), (30, 1), (31, 2), (0, 3), (0, 4), (0, 5), (0, 6)]]]


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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值