当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python Calendar monthdayscalendar()用法及代码示例


Python Calendar.monthdayscalendar() 方法

Calendar.monthdayscalendar() 方法是内置的方法CalendarcalendarPython 中的模块。它使用此类的实例并返回给定年份给定月份中的周列表作为完整周。周是七个数字的列表。如果一周中某一天的值为 0,则表示它不属于该月。

模块:

    import calendar

类:

    from calendar import Calendar

用法:

    monthdayscalendar(year, month)

参数:

  • year:它是一个必需参数,它指定日历的年份。
  • month:它是一个必需参数,它指定日历的月份。

返回值:

这个方法的返回类型是<class 'list'>,它将给定月份中的周列表作为完整周返回,即,每周由 7 个值组成。这里有 0 值的天意味着他们不在那个月。

例:

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

# import class
import calendar

# Creating Calendar Instance
cal = calendar.Calendar()
# default firstweekday =0
year = 2011
month = 11

print("Days outside of the month are 0")
print("Weekwise calendar of November 2011 with first weekday as Monday")
print(cal.monthdayscalendar(year, month))
print()
# always full weeks are listed.

# set the firstweekday to 3
cal = calendar.Calendar(firstweekday = 3)
year = 1998
month = 8

print("Weekwise calendar of August 1998 with first weekday as Thursday")
print(cal.monthdayscalendar(year, month))
print()

输出

Days outside of the month are 0
Weekwise calendar of November 2011 with first weekday as Monday
[
    [0, 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, 0, 0, 0, 0]
]

Weekwise calendar of August 1998 with first weekday as Thursday
[
    [0, 0, 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, 31, 0, 0]
]


相关用法


注:本文由纯净天空筛选整理自 Python Calendar Class | monthdayscalendar() Method with Example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。