Hướng dẫn 03/03/2021 Python 03/03/2021 Python
Trong bài viết này, bạn sẽ học cách có được tổng số thứ Hai hoặc bất kỳ ngày nào khác trong tuần trong tháng ở Python. Có nhiều cách khác nhau để có được tổng số thứ Hai hoặc bất kỳ ngày trong tuần nào khác trong tháng ở Python. & NBSP;
Dưới đây là các ví dụ để có được tổng số thứ Hai hoặc bất kỳ ngày trong tuần nào khác trong tháng ở Python. & NBSP;
Ví dụ 1: Sử dụng mô -đun Lịch và DateTime
Trong ví dụ này, chúng tôi đã sử dụng calendar.monthcalendar() Method từ mô -đun lịch và cho DateTime hiện tại, chúng tôi đã sử dụng mô -đun datetime. Trong ví dụ này, chúng tôi tìm thấy tổng số thứ Hai trong tháng nhưng bạn cũng có thể tìm thấy tổng số của bất kỳ ngày nào khác trong tháng. Bạn phải thay thế chỉ mục của mảng từ 0 đến 6. Ví dụ,calendar.monthcalendar() method from the calendar module and for the current DateTime, we used the datetime module. In this example, we find the total number of Mondays in the month but you can also find the total number of any other weekdays in the month. You have to replace the index of the array from 0 to 6. For example,
Đối với thứ Hai, chúng tôi đã sử dụng ____10, vào thứ ba, chúng tôi có thể sử dụng ____ 11, & nbsp; Đối với thứ Tư, chúng ta có thể sử dụng ____ 12, & nbsp; Và như vậy và giống như cách bạn có thể thay thế giá trị chỉ mục cho bất kỳ ngày trong tuần nào khác.Monday, we used # Import Module import datetime today = datetime.date.today() day = datetime.date(today.year, today.month, 1) single_day = datetime.timedelta(days=1) TotalMondays = 0 while day.month == today.month: if day.weekday() == 0: TotalMondays += 1 day += single_day # Print Output print ("Total Mondays in the Month: ", TotalMondays) 0, for Tuesday, we can use # Import Module import datetime today = datetime.date.today() day = datetime.date(today.year, today.month, 1) single_day = datetime.timedelta(days=1) TotalMondays = 0 while day.month == today.month: if day.weekday() == 0: TotalMondays += 1 day += single_day # Print Output print ("Total Mondays in the Month: ", TotalMondays) 1, for Wednesday, we can use# Import Module import datetime today = datetime.date.today() day = datetime.date(today.year, today.month, 1) single_day = datetime.timedelta(days=1) TotalMondays = 0 while day.month == today.month: if day.weekday() == 0: TotalMondays += 1 day += single_day # Print Output print ("Total Mondays in the Month: ", TotalMondays) 2, and so on and the same way you can replace the index value for any other weekdays.
Dưới đây là ví dụ để có được tổng số thứ Hai trong tháng hoặc bất kỳ ngày trong tuần nào khác.
# Import Module import calendar from datetime import datetime TotalMondays = len([1 for i in calendar.monthcalendar(datetime.now().year, datetime.now().month) if i[0] != 0]) # print Output print("Total Mondays in the Month: ",TotalMondays)
Tổng số thứ Hai trong tháng: & nbsp; 5
Ví dụ 2: Sử dụng mô -đun DateTime và trong khi vòng lặp
Trong ví dụ này, chúng tôi đã sử dụng mô -đun datetime cho DateTime hiện tại. Trong ví dụ này, chúng tôi tìm thấy tổng số thứ Hai trong tháng nhưng bạn cũng có thể tìm thấy tổng số của bất kỳ ngày nào khác trong tháng. Bạn phải gán giá trị # Import Module import datetime today = datetime.date.today() day = datetime.date(today.year, today.month, 1) single_day = datetime.timedelta(days=1) TotalMondays = 0 while day.month == today.month: if day.weekday() == 0: TotalMondays += 1 day += single_day # Print Output print ("Total Mondays in the Month: ", TotalMondays) 4 từ 0 đến 6. Ví dụ: vào thứ Hai, chúng tôi đã sử dụng # Import Module import datetime today = datetime.date.today() day = datetime.date(today.year, today.month, 1) single_day = datetime.timedelta(days=1) TotalMondays = 0 while day.month == today.month: if day.weekday() == 0: TotalMondays += 1 day += single_day # Print Output print ("Total Mondays in the Month: ", TotalMondays) 5, cho thứ ba, chúng tôi có thể sử dụng ________ 16, & nbsp; Đối với thứ Tư, chúng ta có thể sử dụng ____ 17, & nbsp; Và như vậy và giống như cách bạn có thể gán giá trị # Import Module import datetime today = datetime.date.today() day = datetime.date(today.year, today.month, 1) single_day = datetime.timedelta(days=1) TotalMondays = 0 while day.month == today.month: if day.weekday() == 0: TotalMondays += 1 day += single_day # Print Output print ("Total Mondays in the Month: ", TotalMondays) 4 cho bất kỳ ngày trong tuần nào khác.datetime module for the current DateTime. In this example, we find the total number of Mondays in the month but you can also find the total number of any other weekdays in the month. You have to assign # Import Module import datetime today = datetime.date.today() day = datetime.date(today.year, today.month, 1) single_day = datetime.timedelta(days=1) TotalMondays = 0 while day.month == today.month: if day.weekday() == 0: TotalMondays += 1 day += single_day # Print Output print ("Total Mondays in the Month: ", TotalMondays) 4 value from 0 to 6. For example, For Monday, we used # Import Module import datetime today = datetime.date.today() day = datetime.date(today.year, today.month, 1) single_day = datetime.timedelta(days=1) TotalMondays = 0 while day.month == today.month: if day.weekday() == 0: TotalMondays += 1 day += single_day # Print Output print ("Total Mondays in the Month: ", TotalMondays) 5, for Tuesday, we can use # Import Module import datetime today = datetime.date.today() day = datetime.date(today.year, today.month, 1) single_day = datetime.timedelta(days=1) TotalMondays = 0 while day.month == today.month: if day.weekday() == 0: TotalMondays += 1 day += single_day # Print Output print ("Total Mondays in the Month: ", TotalMondays) 6, for Wednesday, we can use# Import Module import datetime today = datetime.date.today() day = datetime.date(today.year, today.month, 1) single_day = datetime.timedelta(days=1) TotalMondays = 0 while day.month == today.month: if day.weekday() == 0: TotalMondays += 1 day += single_day # Print Output print ("Total Mondays in the Month: ", TotalMondays) 7, and so on and the same way you can assign # Import Module import datetime today = datetime.date.today() day = datetime.date(today.year, today.month, 1) single_day = datetime.timedelta(days=1) TotalMondays = 0 while day.month == today.month: if day.weekday() == 0: TotalMondays += 1 day += single_day # Print Output print ("Total Mondays in the Month: ", TotalMondays) 4 value for any other weekdays.
Dưới đây là ví dụ để có được tổng số thứ Hai trong tháng hoặc bất kỳ ngày trong tuần nào khác.
# Import Module import datetime today = datetime.date.today() day = datetime.date(today.year, today.month, 1) single_day = datetime.timedelta(days=1) TotalMondays = 0 while day.month == today.month: if day.weekday() == 0: TotalMondays += 1 day += single_day # Print Output print ("Total Mondays in the Month: ", TotalMondays)
Tổng số thứ Hai trong tháng: & nbsp; 5
Ví dụ 2: Sử dụng mô -đun DateTime và trong khi vòng lặp
Trong ví dụ này, chúng tôi đã sử dụng mô -đun datetime cho DateTime hiện tại. Trong ví dụ này, chúng tôi tìm thấy tổng số thứ Hai trong tháng nhưng bạn cũng có thể tìm thấy tổng số của bất kỳ ngày nào khác trong tháng. Bạn phải gán giá trị # Import Module import datetime today = datetime.date.today() day = datetime.date(today.year, today.month, 1) single_day = datetime.timedelta(days=1) TotalMondays = 0 while day.month == today.month: if day.weekday() == 0: TotalMondays += 1 day += single_day # Print Output print ("Total Mondays in the Month: ", TotalMondays) 4 từ 0 đến 6. Ví dụ: vào thứ Hai, chúng tôi đã sử dụng # Import Module import datetime today = datetime.date.today() day = datetime.date(today.year, today.month, 1) single_day = datetime.timedelta(days=1) TotalMondays = 0 while day.month == today.month: if day.weekday() == 0: TotalMondays += 1 day += single_day # Print Output print ("Total Mondays in the Month: ", TotalMondays) 5, cho thứ ba, chúng tôi có thể sử dụng ________ 16, & nbsp; Đối với thứ Tư, chúng ta có thể sử dụng ____ 17, & nbsp; Và như vậy và giống như cách bạn có thể gán giá trị # Import Module import datetime today = datetime.date.today() day = datetime.date(today.year, today.month, 1) single_day = datetime.timedelta(days=1) TotalMondays = 0 while day.month == today.month: if day.weekday() == 0: TotalMondays += 1 day += single_day # Print Output print ("Total Mondays in the Month: ", TotalMondays) 4 cho bất kỳ ngày trong tuần nào khác.
Tôi mất: (tiết kiệm phải lo lắng về việc ở đúng tháng, v.v.)
from calendar import weekday, monthrange, SUNDAY y, m = 2012, 10 days = [weekday(y, m, d+1) for d in range(*monthrange(y, m))] print days.count(SUNDAY)Hoặc, như @mgilson đã chỉ ra, bạn có thể loại bỏ danh sách-comp và kết thúc tất cả như một trình tạo:
sum(1 for d in range(*monthrange(y,m)) if weekday(y,m,d+1)==SUNDAY)Và tôi cho rằng, bạn có thể ném vào:
from collections import Counter days = Counter(weekday(y, m, d + 1) for d in range(*monthrange(y, m))) print days[SUNDAY]Python DateTime: Bài tập-24 với giải pháp
Viết một chương trình Python để đếm số ngày thứ Hai của ngày đầu tiên của tháng từ năm 2015 đến 2016.
Giải pháp mẫu::
Mã Python:
import datetime from datetime import datetime monday1 = 0 months = range(1,13) for year in range(2015, 2017): for month in months: if datetime(year, month, 1).weekday() == 0: monday1 += 1 print(monday1)Đầu ra mẫu:
3Flowchart:
Trực quan hóa thực thi mã Python:
Công cụ sau đây trực quan hóa những gì máy tính đang làm từng bước khi nó thực hiện chương trình đã nói:
Trình chỉnh sửa mã Python:
Đóng góp mã và nhận xét của bạn thông qua Disqus.
Trước đây: Viết chương trình Python để thêm một tháng với một ngày được chỉ định.
Write a Python program to add a month with a specified date.
Next: Write a Python program to print a string five times, delay three seconds.
Python: Lời khuyên trong ngày
Làm cho regex có thể đọc được:
import re from functools import partial norm_space = partial(res.sub, r'\s+', ' ') norm_space(' a b\tc d') 'a b c d'