Vấn đề với một lớp có nhiều phiếu bầu nhất là nó không hoạt động.one-liner with the most votes is it doesn't work.
Tuy nhiên, nó có thể được sử dụng làm cơ sở để tinh chỉnh:
Bạn thấy đây là những gì bạn nhận được:
c = calendar.Calendar[calendar.SUNDAY].monthdatescalendar[2018, 7]
for c2 in c:
print[c2[0]]
2018-07-01
2018-07-08
2018-07-15
2018-07-22
2018-07-29
c = calendar.Calendar[calendar.SUNDAY].monthdatescalendar[2018, 8]
for c2 in c:
print[c2[0]]
2018-07-29
2018-08-05
2018-08-12
2018-08-19
2018-08-26
Nếu bạn nghĩ về nó, nó đang cố gắng tổ chức lịch thành danh sách lồng nhau để in một tuần có giá trị ngày một lần. Vì vậy, Stragglers từ những tháng khác đi vào chơi. Bằng cách sử dụng một danh sách mới các ngày hợp lệ rơi vào tháng - điều này thực hiện thủ thuật.
Trả lời với danh sách được nối thêm
import calendar
import datetime
def get_nth_DOW_for_YY_MM[dow, yy, mm, nth] -> datetime.date:
#dow - Python Cal - 6 Sun 0 Mon ... 5 Sat
#nth is 1 based... -1. is ok for last.
i = -1 if nth == -1 or nth == 5 else nth -1
valid_days = []
for d in calendar.Calendar[dow].monthdatescalendar[yy, mm]:
if d[0].month == mm:
valid_days.append[d[0]]
return valid_days[i]
Vì vậy, đây là cách nó có thể được gọi là:
firstSundayInJuly2018 = get_nth_DOW_for_YY_MM[calendar.SUNDAY, 2018, 7, 1]
firstSundayInAugust2018 = get_nth_DOW_for_YY_MM[calendar.SUNDAY, 2018, 8, 1]
print[firstSundayInJuly2018]
print[firstSundayInAugust2018]
Và đây là đầu ra:
2018-07-01
2018-08-05
2018-07-01
2018-07-08
2018-07-15
2018-07-22
2018-07-29
0 có thể được tái cấu trúc bằng cách sử dụng các biểu thức lambda như vậy:Trả lời với sự tái cấu trúc biểu hiện Lambda
import calendar
import datetime
def get_nth_DOW_for_YY_MM[dow, yy, mm, nth] -> datetime.date:
#dow - Python Cal - 6 Sun 0 Mon ... 5 Sat
#nth is 1 based... -1. is ok for last.
i = -1 if nth == -1 or nth == 5 else nth -1
return list[filter[lambda x: x.month == mm, \
list[map[lambda x: x[0], \
calendar.Calendar[dow].monthdatescalendar[yy, mm] \
]] \
]][i]
Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc
0 | Mục lục |
1 | Ngày trong tuần |
2 | Thứ hai |
3 | Thứ ba |
4 | Thứ Tư |
5 | thứ năm |
6 | Thứ sáu |
Examples:
Thứ bảy test_date = datetime.datetime[2017, 3, 14], weekday_idx = 4
Chủ nhật 2017-03-17
Đầu vào: test_date = datetime.datetime [2017, 3, 14], weekday_idx = 4 14 March is Tuesday, i.e 1 weekday, 4th weekday is a Friday, i.e 17 March.
Đầu ra: 2017-03-17 & NBSP; test_date = datetime.datetime[2017, 3, 12], weekday_idx = 5
Giải thích: 14 tháng 3 là thứ ba, tức là 1 ngày trong tuần, ngày thứ 4 là thứ Sáu, tức là 17 tháng 3. 2017-03-18
Đầu vào: test_date = datetime.datetime [2017, 3, 12], weekay_idx = 5 12 March is Sunday, i.e 6th weekday, 5th weekday in next week is a Saturday, i.e 18 March.
Đầu ra: 2017-03-18timedelta[] + weekday[]
Giải thích: 12 tháng 3 là Chủ nhật, tức là ngày thứ 6, ngày thứ 5 trong tuần tới là thứ bảy, tức là 18 tháng 3.
Python3
Phương thức số 1: Sử dụng TimedelTa [] + Day Day []
Trong đó, chúng tôi trừ ngày trong tuần từ chỉ số ngày trong tuần và sau đó kiểm tra chỉ số cần thiết được trích xuất, sau đó là ngày cần thiết, nếu âm được tính tổng với 7 và sau đó số kết quả được thêm vào ngày hiện tại bằng thời gian sử dụng Timedelta [].
2018-07-01
2018-07-08
2018-07-15
2018-07-22
2018-07-29
1 2018-07-01
2018-07-08
2018-07-15
2018-07-22
2018-07-29
22018-07-01
2018-07-08
2018-07-15
2018-07-22
2018-07-29
32018-07-01
2018-07-08
2018-07-15
2018-07-22
2018-07-29
4 2018-07-01
2018-07-08
2018-07-15
2018-07-22
2018-07-29
52018-07-01
2018-07-08
2018-07-15
2018-07-22
2018-07-29
62018-07-01
2018-07-08
2018-07-15
2018-07-22
2018-07-29
72018-07-01
2018-07-08
2018-07-15
2018-07-22
2018-07-29
82018-07-01
2018-07-08
2018-07-15
2018-07-22
2018-07-29
7c = calendar.Calendar[calendar.SUNDAY].monthdatescalendar[2018, 8]
for c2 in c:
print[c2[0]]
0c = calendar.Calendar[calendar.SUNDAY].monthdatescalendar[2018, 8]
for c2 in c:
print[c2[0]]
1c = calendar.Calendar[calendar.SUNDAY].monthdatescalendar[2018, 8]
for c2 in c:
print[c2[0]]
2c = calendar.Calendar[calendar.SUNDAY].monthdatescalendar[2018, 8]
for c2 in c:
print[c2[0]]
3c = calendar.Calendar[calendar.SUNDAY].monthdatescalendar[2018, 8]
for c2 in c:
print[c2[0]]
4 c = calendar.Calendar[calendar.SUNDAY].monthdatescalendar[2018, 8]
for c2 in c:
print[c2[0]]
5 c = calendar.Calendar[calendar.SUNDAY].monthdatescalendar[2018, 8]
for c2 in c:
print[c2[0]]
6c = calendar.Calendar[calendar.SUNDAY].monthdatescalendar[2018, 8]
for c2 in c:
print[c2[0]]
7c = calendar.Calendar[calendar.SUNDAY].monthdatescalendar[2018, 8]
for c2 in c:
print[c2[0]]
8c = calendar.Calendar[calendar.SUNDAY].monthdatescalendar[2018, 8]
for c2 in c:
print[c2[0]]
92018-07-29
2018-08-05
2018-08-12
2018-08-19
2018-08-26
02018-07-01
2018-07-08
2018-07-15
2018-07-22
2018-07-29
4 2018-07-29
2018-08-05
2018-08-12
2018-08-19
2018-08-26
22018-07-29
2018-08-05
2018-08-12
2018-08-19
2018-08-26
32018-07-01
2018-07-08
2018-07-15
2018-07-22
2018-07-29
4 2018-07-29
2018-08-05
2018-08-12
2018-08-19
2018-08-26
02018-07-29
2018-08-05
2018-08-12
2018-08-19
2018-08-26
6 2018-07-29
2018-08-05
2018-08-12
2018-08-19
2018-08-26
72018-07-29
2018-08-05
2018-08-12
2018-08-19
2018-08-26
8 2018-07-29
2018-08-05
2018-08-12
2018-08-19
2018-08-26
92018-07-01
2018-07-08
2018-07-15
2018-07-22
2018-07-29
4 import calendar
import datetime
def get_nth_DOW_for_YY_MM[dow, yy, mm, nth] -> datetime.date:
#dow - Python Cal - 6 Sun 0 Mon ... 5 Sat
#nth is 1 based... -1. is ok for last.
i = -1 if nth == -1 or nth == 5 else nth -1
valid_days = []
for d in calendar.Calendar[dow].monthdatescalendar[yy, mm]:
if d[0].month == mm:
valid_days.append[d[0]]
return valid_days[i]
1import calendar
import datetime
def get_nth_DOW_for_YY_MM[dow, yy, mm, nth] -> datetime.date:
#dow - Python Cal - 6 Sun 0 Mon ... 5 Sat
#nth is 1 based... -1. is ok for last.
i = -1 if nth == -1 or nth == 5 else nth -1
valid_days = []
for d in calendar.Calendar[dow].monthdatescalendar[yy, mm]:
if d[0].month == mm:
valid_days.append[d[0]]
return valid_days[i]
2
c = calendar.Calendar[calendar.SUNDAY].monthdatescalendar[2018, 8]
for c2 in c:
print[c2[0]]
2c = calendar.Calendar[calendar.SUNDAY].monthdatescalendar[2018, 8]
for c2 in c:
print[c2[0]]
3firstSundayInJuly2018 = get_nth_DOW_for_YY_MM[calendar.SUNDAY, 2018, 7, 1]
firstSundayInAugust2018 = get_nth_DOW_for_YY_MM[calendar.SUNDAY, 2018, 8, 1]
print[firstSundayInJuly2018]
print[firstSundayInAugust2018]
5 c = calendar.Calendar[calendar.SUNDAY].monthdatescalendar[2018, 8]
for c2 in c:
print[c2[0]]
5 c = calendar.Calendar[calendar.SUNDAY].monthdatescalendar[2018, 8]
for c2 in c:
print[c2[0]]
6firstSundayInJuly2018 = get_nth_DOW_for_YY_MM[calendar.SUNDAY, 2018, 7, 1]
firstSundayInAugust2018 = get_nth_DOW_for_YY_MM[calendar.SUNDAY, 2018, 8, 1]
print[firstSundayInJuly2018]
print[firstSundayInAugust2018]
8c = calendar.Calendar[calendar.SUNDAY].monthdatescalendar[2018, 8]
for c2 in c:
print[c2[0]]
8c = calendar.Calendar[calendar.SUNDAY].monthdatescalendar[2018, 8]
for c2 in c:
print[c2[0]]
9Output:
The original date is : 2017-03-14 Next date of required weekday : 2017-03-17
3import calendar
import datetime
def get_nth_DOW_for_YY_MM[dow, yy, mm, nth] -> datetime.date:
#dow - Python Cal - 6 Sun 0 Mon ... 5 Sat
#nth is 1 based... -1. is ok for last.
i = -1 if nth == -1 or nth == 5 else nth -1
valid_days = []
for d in calendar.Calendar[dow].monthdatescalendar[yy, mm]:
if d[0].month == mm:
valid_days.append[d[0]]
return valid_days[i]
32018-07-29
2018-08-05
2018-08-12
2018-08-19
2018-08-26
5c = calendar.Calendar[calendar.SUNDAY].monthdatescalendar[2018, 8]
for c2 in c:
print[c2[0]]
4 2018-07-01
2018-07-08
2018-07-15
2018-07-22
2018-07-29
7lambda functionimport calendar
import datetime
def get_nth_DOW_for_YY_MM[dow, yy, mm, nth] -> datetime.date:
#dow - Python Cal - 6 Sun 0 Mon ... 5 Sat
#nth is 1 based... -1. is ok for last.
i = -1 if nth == -1 or nth == 5 else nth -1
valid_days = []
for d in calendar.Calendar[dow].monthdatescalendar[yy, mm]:
if d[0].month == mm:
valid_days.append[d[0]]
return valid_days[i]
import calendar
import datetime
def get_nth_DOW_for_YY_MM[dow, yy, mm, nth] -> datetime.date:
#dow - Python Cal - 6 Sun 0 Mon ... 5 Sat
#nth is 1 based... -1. is ok for last.
i = -1 if nth == -1 or nth == 5 else nth -1
valid_days = []
for d in calendar.Calendar[dow].monthdatescalendar[yy, mm]:
if d[0].month == mm:
valid_days.append[d[0]]
return valid_days[i]
82018-07-01
2018-07-08
2018-07-15
2018-07-22
2018-07-29
4 2018-07-01
2018-07-08
2018-07-15
2018-07-22
2018-07-29
3c = calendar.Calendar[calendar.SUNDAY].monthdatescalendar[2018, 8]
for c2 in c:
print[c2[0]]
5 firstSundayInJuly2018 = get_nth_DOW_for_YY_MM[calendar.SUNDAY, 2018, 7, 1]
firstSundayInAugust2018 = get_nth_DOW_for_YY_MM[calendar.SUNDAY, 2018, 8, 1]
print[firstSundayInJuly2018]
print[firstSundayInAugust2018]
2Python3
Phương thức số 1: Sử dụng TimedelTa [] + Day Day []
Trong đó, chúng tôi trừ ngày trong tuần từ chỉ số ngày trong tuần và sau đó kiểm tra chỉ số cần thiết được trích xuất, sau đó là ngày cần thiết, nếu âm được tính tổng với 7 và sau đó số kết quả được thêm vào ngày hiện tại bằng thời gian sử dụng Timedelta [].
2018-07-01
2018-07-08
2018-07-15
2018-07-22
2018-07-29
1 2018-07-01
2018-07-08
2018-07-15
2018-07-22
2018-07-29
22018-07-01
2018-07-08
2018-07-15
2018-07-22
2018-07-29
32018-07-01
2018-07-08
2018-07-15
2018-07-22
2018-07-29
4 2018-07-01
2018-07-08
2018-07-15
2018-07-22
2018-07-29
52018-07-01
2018-07-08
2018-07-15
2018-07-22
2018-07-29
62018-07-01
2018-07-08
2018-07-15
2018-07-22
2018-07-29
72018-07-01
2018-07-08
2018-07-15
2018-07-22
2018-07-29
82018-07-01
2018-07-08
2018-07-15
2018-07-22
2018-07-29
7c = calendar.Calendar[calendar.SUNDAY].monthdatescalendar[2018, 8]
for c2 in c:
print[c2[0]]
0c = calendar.Calendar[calendar.SUNDAY].monthdatescalendar[2018, 8]
for c2 in c:
print[c2[0]]
1The original date is : 2017-03-14 Next date of required weekday : 2017-03-173
The original date is : 2017-03-14 Next date of required weekday : 2017-03-174
The original date is : 2017-03-14 Next date of required weekday : 2017-03-175
2018-07-01
2018-07-08
2018-07-15
2018-07-22
2018-07-29
3c = calendar.Calendar[calendar.SUNDAY].monthdatescalendar[2018, 8]
for c2 in c:
print[c2[0]]
5 The original date is : 2017-03-14 Next date of required weekday : 2017-03-178
import calendar
import datetime
def get_nth_DOW_for_YY_MM[dow, yy, mm, nth] -> datetime.date:
#dow - Python Cal - 6 Sun 0 Mon ... 5 Sat
#nth is 1 based... -1. is ok for last.
i = -1 if nth == -1 or nth == 5 else nth -1
valid_days = []
for d in calendar.Calendar[dow].monthdatescalendar[yy, mm]:
if d[0].month == mm:
valid_days.append[d[0]]
return valid_days[i]
3The original date is : 2017-03-14 Next date of required weekday : 2017-03-170
2018-07-01
2018-07-08
2018-07-15
2018-07-22
2018-07-29
4The original date is : 2017-03-14 Next date of required weekday : 2017-03-172___
import calendar
import datetime
def get_nth_DOW_for_YY_MM[dow, yy, mm, nth] -> datetime.date:
#dow - Python Cal - 6 Sun 0 Mon ... 5 Sat
#nth is 1 based... -1. is ok for last.
i = -1 if nth == -1 or nth == 5 else nth -1
valid_days = []
for d in calendar.Calendar[dow].monthdatescalendar[yy, mm]:
if d[0].month == mm:
valid_days.append[d[0]]
return valid_days[i]
82018-07-01
2018-07-08
2018-07-15
2018-07-22
2018-07-29
4 2018-07-01
2018-07-08
2018-07-15
2018-07-22
2018-07-29
03c = calendar.Calendar[calendar.SUNDAY].monthdatescalendar[2018, 8]
for c2 in c:
print[c2[0]]
2c = calendar.Calendar[calendar.SUNDAY].monthdatescalendar[2018, 8]
for c2 in c:
print[c2[0]]
3firstSundayInJuly2018 = get_nth_DOW_for_YY_MM[calendar.SUNDAY, 2018, 7, 1]
firstSundayInAugust2018 = get_nth_DOW_for_YY_MM[calendar.SUNDAY, 2018, 8, 1]
print[firstSundayInJuly2018]
print[firstSundayInAugust2018]
5 c = calendar.Calendar[calendar.SUNDAY].monthdatescalendar[2018, 8]
for c2 in c:
print[c2[0]]
5 c = calendar.Calendar[calendar.SUNDAY].monthdatescalendar[2018, 8]
for c2 in c:
print[c2[0]]
6firstSundayInJuly2018 = get_nth_DOW_for_YY_MM[calendar.SUNDAY, 2018, 7, 1]
firstSundayInAugust2018 = get_nth_DOW_for_YY_MM[calendar.SUNDAY, 2018, 8, 1]
print[firstSundayInJuly2018]
print[firstSundayInAugust2018]
8c = calendar.Calendar[calendar.SUNDAY].monthdatescalendar[2018, 8]
for c2 in c:
print[c2[0]]
8c = calendar.Calendar[calendar.SUNDAY].monthdatescalendar[2018, 8]
for c2 in c:
print[c2[0]]
9Output:
The original date is : 2017-03-14 Next date of required weekday : 2017-03-17