首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >一周中每天有多少人在给定的时间工作,python

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

Stack Overflow用户
提问于 2020-10-23 06:02:33
回答 1查看 60关注 0票数 2

我正在尝试创建这种人员配备砂粒,使我的管理工作在工作中更容易。“天”包含一个星期。

天= M,T,W,Th,F

days = 0,1,1,1,1表示除星期一外每天都工作。

如果该值为2,则表示它们执行特殊的轮班。

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

我得到了星期一,但我知道有一个更好的方法,也许是使用函数,来打印所有的日子。我已经玩了很久了,但我还是弄不明白。提前谢谢你!我非常尊重你们所有的专家!

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-23 09:22:19

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

代码语言:javascript
复制
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()
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64491142

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档