Python计算旋转工作班次的天数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python计算旋转工作班次的天数相关的知识,希望对你有一定的参考价值。

我是python的新手,我无法弄清楚任何可以解决这个问题的策略或现有模块。我想制作一个简单的程序,如果将来的某一天是休息一天或者不是基于以下时间表(2018年7月这个星期的实际时间表)将返回:

Week 1: 1:Sun:off 2:Mon   3:Tues   4:Wed:off   5:Thur   6:Fri   7:Sat

Week 2: 8:Sun:  9:Mon:off 10:Tues  11:Wed     12:Thur  13:Fri  14:Sat:off

Week 3:15:Sun:off  16:Mon   17:Tues  18:Wed:off  19:Thur  20:Fri  21:Sat 

Week 4:22:Sun 23:Mon:off   24:Tues  25:Wed     26:Thur  27:Fri  28:Sat:off
import datetime

sched1 = {'Mon':'working','Tues':'working','Wed':'off','Thur':'working','Fri':'working','Sat':'working','Sun':'off'}
sched2 = {'Mon':'off','Tues':'working','Wed':'working','Thur':'working','Fri':'working','Sat':'off','Sun':'working'}

pickdate1 = int(input("Enter a date in the year (YYYY)): "))
pickdate2 = int(input("Enter a date in the year (MM): "))
pickdate3 = int(input("Enter a date in the year (DD): "))


date = datetime.date(pickdate1,pickdate2,pickdate3)
weekno = datetime.date(pickdate1,pickdate2,pickdate3).isocalendar()[1]
weekday = datetime.date.isoweekday(date)

if weekno % 2 == 0:
    print (sched2[weekday])

elif weekno % 2 != 0:
    print (sched1[weekday])
答案

这将生成每周计划列表。每周的时间表是从周一开始到周日的列表。星期一到星期日的选择只是为了与datetimecalendar模块中的Python日期顺序保持一致。

weeks = []
num_weeks = 4 # how many weeks to schedule
days_off = [
    (2, 6),   # sunday and wednesday
    (0, 5),   # monday and saturday
]

for week in range(num_weeks):
    days_off_this_week = days_off[week % len(days_off)]
    weeks.append([bool(i not in days_off_this_week) for i in range(7)])

这里的关键是使用%算子(week % len(days_off))而不是//来确定我们应该使用哪个星期的时间表。因为我们使用len(days_off),我们可以通过简单地向days_off添加一个条目来使我们的2周轮换3或4周轮换,并且日历仍然可以工作。

将星期几存储为整数,将开/关存储为布尔值,为我们以后如何组织此信息进行显示提供了很大的灵活性。我们可以使用日历模块轻松打印日程表:

import calendar
from collections import OrderedDict
cal = calendar.Calendar(calendar.SUNDAY)

for week in weeks:
    print(OrderedDict([(calendar.day_abbr[i], ("On" if week[i] else "Off")) for i in cal.iterweekdays()]))

要在我们的显示中更改每周的第一个工作日,我们只需将一周的不同日期传递给calendar.Calendar(例如calendar.Calendar(calendar.MONDAY))。

这并不考虑实际日期,但我很乐意补充一点,如果它有用。

另一答案
import datetime

sched1 = {'Mon':'working','Tues':'working','Wed':'off','Thur':'working','Fri':'working','Sat':'working','Sun':'off'}
sched2 = {'Mon':'off','Tues':'working','Wed':'working','Thur':'working','Fri':'working','Sat':'off','Sun':'working'}

pickdate1 = int(input("Enter a date in the year (YYYY)): "))
pickdate2 = int(input("Enter a date in the year (MM): "))
pickdate3 = int(input("Enter a date in the year (DD): "))


date = datetime.date(pickdate1,pickdate2,pickdate3)
weekno = datetime.date(pickdate1,pickdate2,pickdate3).isocalendar()[1]
weekday = datetime.date.isoweekday(date)


if weekno % 2 == 0:
    if weekday == 1:
        print ("You are: %s" % (sched2['Mon']))
    elif weekday == 2:
        print ("You are: %s" % (sched2['Tues']))
    elif weekday == 3:
        print ("You are: %s" % (sched2['Wed']))
    elif weekday == 4:
        print ("You are: %s" %  (sched2['Thur']))
    elif weekday == 5:
        print ("You are: %s" %  (sched2['Fri']))
    elif weekday == 6:
        print ("You are: %s" %  (sched2['Sat']))
    elif weekday == 7:
        print ("You are: %s" %  (sched2['Sun']))
elif weekno % 2 != 0:
    if weekday == 1:
        print ("You are: %s" %  (sched1['Mon']))
    elif weekday == 2:
        print ("You are: %s" %  (sched1['Tues']))
    elif weekday == 3:
        print ("You are: %s" %  (sched1['Wed']))
    elif weekday == 4:
        print ("You are: %s" %  (sched1['Thur']))
    elif weekday == 5:
        print ("You are: %s" %  (sched1['Fri']))
    elif weekday == 6:
        print ("You are: %s" %  (sched1['Sat']))
    elif weekday == 7:
        print ("You are: %s" %  (sched1['Sun']))

以上是关于Python计算旋转工作班次的天数的主要内容,如果未能解决你的问题,请参考以下文章

Vuejs:计算属性不更新视图

使用从循环内的代码片段中提取的函数避免代码冗余/计算开销

Python:日薪工资计算

Python 利用函数列表来实现计算天数

excel表格怎么计算两个时间之间的工作日天数

输入日期自动计算天数?