首页 > 解决方案 > 有多少人在一周中的每一天的给定时间工作,python

问题描述

我正在尝试创建这种人员配备的勇气,以使我的管理员在工作中更轻松。'days' 包含一周。

天 = [M, T, W, Th, F]

days = [0, 1, 1, 1, 1] 表示他/她每天工作,周一除外。

如果值为 2,则表示他们工作特殊班次。

他/她从 start_time 工作到 end_time - 例如 wakana 每天工作 0600-1400。她/他在值为 2 的日子从 special_start 到 special_end 工作,例如 eleonor 周一和周五在 0700-1900 工作,周三在 0700-1500 工作。

我星期一下来了,但我知道有更好的方法,也许使用函数来打印所有天。我现在一直在玩它,但我无法弄清楚。先感谢您!我非常尊重你们所有的专家!

staffing_data = [
        {'name': 'wakana',
        'start_time': 6,
        'end_time': 14,
        'days': [1, 1, 1, 1, 1],
        'special_start': None,
        'special_end': None},

        {'name': 'kate',
        'start_time': 11,
        'end_time': 21,
        'days': [0, 1, 1, 1, 1],
        'special_start': None,
        'special_end': None},

        {'name': 'eleonor',
        'start_time': 7,
        'end_time': 19,
        'days': [1, 0, 2, 0, 1],
        'special_start': 7,
        'special_end': 15}]

at_7 = 0
at_11 = 0
at_15 = 0
at_19 = 0



for person in staffing_data:

    if person['start_time'] <= 7 and person['end_time'] > 7 and person['days'][0] == 1:
        at_7 += 1
    if person['start_time'] <= 11 and person['end_time'] > 11 and person['days'][0] == 1:
        at_11 += 1
    if person['start_time'] <= 15 and person['end_time'] > 15 and person['days'][0] == 1:
        at_15 += 1
    if person['start_time'] <= 19 and person['end_time'] > 19 and person['days'][0] == 1:
        at_19 += 1


print(f"{at_7} at 7")
print(f"{at_11} at 11")
print(f"{at_15} at 15")
print(f"{at_19} at 19")


#Monday Staffing
#2 at 7
#3 at 11
#1 at 15
#0 at 19

标签: pythonpython-3.xfunction

解决方案


您只需要另一个循环来循环日期并存储数据。

staffing_data = [
        {'name': 'wakana',
        'start_time': 6,
        'end_time': 14,
        'days': [1, 1, 1, 1, 1],
        'special_start': None,
        'special_end': None},

        {'name': 'kate',
        'start_time': 11,
        'end_time': 21,
        'days': [0, 1, 1, 1, 1],
        'special_start': None,
        'special_end': None},

        {'name': 'eleonor',
        'start_time': 7,
        'end_time': 19,
        'days': [1, 0, 2, 0, 1],
        'special_start': 7,
        'special_end': 15}]

days = ['M', 'T', 'W', 'Th', 'F']
#result = [{"at_7":0,"at_11":0,"at_15":0,"at_19":0} for _ in range(len(days))]
result = []
for _ in range(len(days)):
    result.append({"at_7":0,"at_11":0,"at_15":0,"at_19":0})
        
 
for person in staffing_data:
    
    for day in range(len(days)):
        start = 'start_time'
        end = 'end_time'
        
        if person['days'][day] == 0:
            continue
        elif person['days'][day] == 2:
            start = 'special_start'
            end = 'special_end'
            
        if person[start] <= 7 and person[end] > 7:
            result[day]["at_7"] += 1
        if person[start] <= 11 and person[end] > 11:
            result[day]["at_11"] += 1
        if person[start] <= 15 and person[end] > 15:
            result[day]["at_15"] += 1
        if person[start] <= 19 and person[end] > 19:
            result[day]["at_19"] += 1

for i in range(len(days)):
    print(days[i])
    print(f"{result[i]['at_7']} at 7")
    print(f"{result[i]['at_11']} at 11")
    print(f"{result[i]['at_15']} at 15")
    print(f"{result[i]['at_19']} at 19")
    print()

推荐阅读