在日历上循环三遍

如何解决在日历上循环三遍

我在一家紧急医疗服务机构工作,我们的救护车轮班运行。 Shift A从1月1日的07:00开始,一直持续到1月2日的07:00 班次B从1月2日的07:00开始,在1月3日的07:00停止。 Shift C从1月3日的07:00开始,并立即在1月4日的07:00停止。

然后该模式从Shift A运行24小时,Shift B运行24小时,Shift C运行24小时重新开始,然后重复该模式。

使用Python 3.7,如何编写一个循环以使A,B和C运行3次? 它需要在24小时内运行,因为我正在对数据进行进一步的统计分析。例如,相对于班次B,班次发生了多少紧急情况。 这是我目前拥有的:

#Who:   David
#What:  Generate a text file showing a calendar date with 24 hours with the shift of A,B or C
#When:  02 September 2020
#Where: Python 3.7
#Why:   The goal is to analyze shift patterns

import datetime

with open("c:\\temp\\calendar_shifts.txt","w") as calendar_dates:
    calendar_dates.write("Key,Calendar,Shift" + "\n")
    date = datetime.datetime(2020,1,6,0)
    shift = 'A'
    i = 0
    while (date < datetime.datetime(2020,15,7,0)):
        date += datetime.timedelta(hours = 1)
        i = i + 1
        calendar_dates.write(str(i) + ',' + str(date)+ "," + str(shift) + "\n")

最终结果如下:

Key,Shift
1,2020-01-01 07:00:00,A 
2,2020-01-01 08:00:00,A 
3,2020-01-01 09:00:00,A 
4,2020-01-01 10:00:00,A 
5,2020-01-01 11:00:00,A 
6,2020-01-01 12:00:00,A 
7,2020-01-01 13:00:00,A 
8,2020-01-01 14:00:00,A 
9,2020-01-01 15:00:00,A 
10,2020-01-01 16:00:00,A 
11,2020-01-01 17:00:00,A 
12,2020-01-01 18:00:00,A 
13,2020-01-01 19:00:00,A 
14,2020-01-01 20:00:00,A 
15,2020-01-01 21:00:00,A 
16,2020-01-01 22:00:00,A 
17,2020-01-01 23:00:00,A 
18,2020-01-02 00:00:00,A 
19,2020-01-02 01:00:00,A 
20,2020-01-02 02:00:00,A 
21,2020-01-02 03:00:00,A 
22,2020-01-02 04:00:00,A 
23,2020-01-02 05:00:00,A 
24,2020-01-02 06:00:00,A 
25,2020-01-02 07:00:00,B 
26,2020-01-02 08:00:00,B 
27,2020-01-02 09:00:00,B 
28,2020-01-02 10:00:00,B 
29,2020-01-02 11:00:00,B 
30,2020-01-02 12:00:00,B 
31,2020-01-02 13:00:00,B 
32,2020-01-02 14:00:00,B 
33,2020-01-02 15:00:00,B 
34,2020-01-02 16:00:00,B 
35,2020-01-02 17:00:00,B 
36,2020-01-02 18:00:00,B 
37,2020-01-02 19:00:00,B 
38,2020-01-02 20:00:00,B 
39,2020-01-02 21:00:00,B 
40,2020-01-02 22:00:00,B 
41,2020-01-02 23:00:00,B 
42,2020-01-03 00:00:00,B 
43,2020-01-03 01:00:00,B 
44,2020-01-03 02:00:00,B 
45,2020-01-03 03:00:00,B 
46,2020-01-03 04:00:00,B 
47,2020-01-03 05:00:00,B 
48,2020-01-03 06:00:00,B 
49,2020-01-03 07:00:00,C 
50,2020-01-03 08:00:00,C 
51,2020-01-03 09:00:00,C 
52,2020-01-03 10:00:00,C 
53,2020-01-03 11:00:00,C 
54,2020-01-03 12:00:00,C 
55,2020-01-03 13:00:00,C 
56,2020-01-03 14:00:00,C 
57,2020-01-03 15:00:00,C 
58,2020-01-03 16:00:00,C 
59,2020-01-03 17:00:00,C 
60,2020-01-03 18:00:00,C 
61,2020-01-03 19:00:00,C 
62,2020-01-03 20:00:00,C 
63,2020-01-03 21:00:00,C 
64,2020-01-03 22:00:00,C 
65,2020-01-03 23:00:00,C 
66,2020-01-04 00:00:00,C 
67,2020-01-04 01:00:00,C 
68,2020-01-04 02:00:00,C 
69,2020-01-04 03:00:00,C 
70,2020-01-04 04:00:00,C 
71,2020-01-04 05:00:00,C 
72,2020-01-04 06:00:00,C 
73,2020-01-04 07:00:00,A 
74,2020-01-04 08:00:00,A 
75,2020-01-04 09:00:00,A 
76,2020-01-04 10:00:00,A 
etc.

解决方法

执行此操作的一种方法是制作一个移位名称数组,然后每24小时使用模算术将移位更改为数组中的下一个条目,以使数组索引保持在范围内:

import datetime

print("Key,Calendar,Shift" + "\n")
date = datetime.datetime(2020,1,6,0)
shifts = ['A','B','C']
num_shifts = len(shifts)
shift = -1   # so that the first increment takes us to the first shift
i = 0
while (date < datetime.datetime(2020,15,7,0)):
    date += datetime.timedelta(hours = 1)
    if i % 24 == 0:
        shift = (shift + 1) % num_shifts
    i = i + 1
    print(str(i) + ',' + str(date)+ "," + str(shifts[shift]) + "\n")

输出:

Key,Shift
1,2020-01-01 07:00:00,A
2,2020-01-01 08:00:00,A
3,2020-01-01 09:00:00,A
4,2020-01-01 10:00:00,A
...
23,2020-01-02 05:00:00,A
24,2020-01-02 06:00:00,A
25,2020-01-02 07:00:00,B
26,2020-01-02 08:00:00,B
...
48,2020-01-03 06:00:00,B
49,2020-01-03 07:00:00,C
...
71,2020-01-04 05:00:00,C
72,2020-01-04 06:00:00,C
73,2020-01-04 07:00:00,A
74,2020-01-04 08:00:00,A
,

谢谢尼克。这有很大帮助。另外要注意的是,我尝试了以下操作,但无法使其循环多次。

with open("c:\\temp\\x.txt","w") as calendar_dates:
    calendar_dates.write("Key,Shift" + "\n")
    date = datetime.datetime(2020,0)

    x = 0
    i = 0
    j = 0
    k = 0

    while (date < datetime.datetime(2020,3,0)):
        while (i < 24):
            shift = 'A'
            i = i + 1
            date += datetime.timedelta(hours = 1)
            x = x + 1
            calendar_dates.write(str(x) + '," + str(shift) + "\n")
        while (j < 24):
            shift = 'B'
            j = j + 1
            date += datetime.timedelta(hours = 1)
            x = x + 1
            calendar_dates.write(str(x) + '," + str(shift) + "\n")
        while (k < 24):
            shift = 'C'
            k = k + 1
            date += datetime.timedelta(hours = 1)
            x = x + 1
            calendar_dates.write(str(x) + '," + str(shift) + "\n")

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res