python - 有多少人在一周中的每一天的给定时间工作,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
解决方案
您只需要另一个循环来循环日期并存储数据。
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()
推荐阅读
- sql - 根据日历日期范围内的日期计算滚动总和
- box2d - 设置 Box2d Body LIBGDX 的原点
- c++ - Pybind11:没有参数的重载方法
- haskell - 具有多种参数类型的多态输入函数
- python - 如何克服一个或多个元素不应用循环条件或使用 python 跳过一个元素?
- c++ - 如何停止 C++ 程序?
- gmail - 垃圾邮件刺客报告 PP_MIME_FAKE_ASCII_TEXT MIME 文本/纯文本声称是 ASCII 但不是
- slack - Slack API 通道名称到对话 ID
- python - 如何在使用熊猫执行数据聚合时忽略某些值?
- python - 能否获取 EC2 实例的公网 IP 地址