當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python calendar yeardays2calendar()用法及代碼示例


日曆模塊允許輸出類似於程序的日曆,並提供與日曆相關的其他有用函數。 “日曆”模塊中定義的函數和類使用理想化的日曆,當前的公曆日曆在兩個方向上都無限期擴展。

Python中的yeardays2calendar()方法用於獲取指定年份的數據。周列表中的條目是天數和工作日數的元組。這個月以外的天數為零。

用法: yeardays2calendar(year, width)

參數: 
year: year of the calendar
width: [Default:3] number of months in each row. 

返回: tuples of day numbers and weekday numbers.

代碼1:


# Python program to demonstrate working 
# of yeardays2calendar() method 
  
# importing calendar module 
import calendar 
  
obj = calendar.Calendar() 
  
year = 2016
# default value of width is 3 
  
# priting with yeardays2calendar 
print(obj.yeardays2calendar(year))

輸出:

[[[[(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)]]

[[(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)]]]]

代碼2:迭代星期列表

# Python program to demonstrate working 
# of yeardays2calendar() method 
  
# importing calendar module 
import calendar 
  
obj = calendar.Calendar() 
  
# iteratign with yeardays2calendar 
for day in obj.yeardays2calendar(2018, 1):
    print(day)

輸出:

[[[(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), (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)]]]



相關用法


注:本文由純淨天空篩選整理自Shivam_k大神的英文原創作品 Python calendar module | yeardays2calendar() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。