python - 有多少人在一周中的每一天的给定时间工作,python

标签 python python-3.x function

我正在尝试创建这种人员配备,以使我的管理员工作更轻松。 “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

最佳答案

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

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

关于python - 有多少人在一周中的每一天的给定时间工作,python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64491142/

相关文章:

javascript - django select2 插件中所有选定项目的列表

javascript - 删除功能不起作用,请帮忙

javascript - 我如何从另一个外部 javascript 文件中的外部 javascript 文件调用函数?

python - 如何将随机森林中选定的特征转换为新列表

python - Django 模板中的数字格式

python - 在 Flask 工厂设置中反射(reflect)不同的数据库

python-3.x - 如果我在一个类中有多个测试并且前面的测试失败了,我如何让它跳过或退出该类而不是测试其余的测试?

python - 使用 PyQt5 在文本框中打印输出语句

javascript - 一键运行 2 个函数 Javascript

python - 属性错误: module 'pandas.compat' has no attribute 'iteritems'