gpt4 book ai didi

Python 日期时间与顺序问题

转载 作者:行者123 更新时间:2023-12-01 01:00:38 26 4
gpt4 key购买 nike

我正在尝试编写一个脚本,其中列表中有 3 个变量。

首先,查看我当前的脚本:

import datetime as dt
import pandas as pd

x = ['jhon doe', 'GX']

y = ['donald ted', 'GY']

Z = ['smith joe', 'GZ']

start_date = dt.datetime(2019, 4,12)
end_date = dt.datetime(2019, 4,21)
daterange = pd.date_range(start_date, end_date)

for date in daterange:
print(date)

我对这样的输出感到困扰:

12/04/2019, jhone doe, GX
13/04/2019, donald ted, GY
14/04/2019, smith jhoe, GZ
15/04/2019, jhone doe, GX
16/04/2019, donald ted, GY
17/04/2019, smith jhoe, GZ
18/04/2019, jhone doe, GX
19/04/2019, donald ted, GY
14/04/2019, smith jhoe, GZ
21/04/2019, jhone doe, GX

如果您看到我的预期输出,就很清楚了。

谁能告诉我这个怎么做?上面给出了 3 个变量。

我觉得没必要写太多

最佳答案

您的工具箱中似乎缺少一个工具:from itertools import period

循环允许您创建一个可以连续遍历列表的迭代器。如果您到达列表末尾,它将返回到开头。

from itertools import cycle

import datetime as dt
import pandas as pd

x = ['jhon doe', 'GX']

y = ['donald ted', 'GY']

z = ['smith joe', 'GZ']
r = (x, y, z)
pool = cycle(r)

start_date = dt.datetime(2019, 4,12)
end_date = dt.datetime(2019, 4,21)
daterange = pd.date_range(start_date, end_date)

for date in daterange:
curent_person = next(pool)
print('{}, {}, {}'.format(date.strftime('%d/%m/%Y'), curent_person[0], curent_person[1]))

输出:

12/04/2019, jhon doe, GX
13/04/2019, donald ted, GY
14/04/2019, smith joe, GZ
15/04/2019, jhon doe, GX
16/04/2019, donald ted, GY
17/04/2019, smith joe, GZ
18/04/2019, jhon doe, GX
19/04/2019, donald ted, GY
20/04/2019, smith joe, GZ
21/04/2019, jhon doe, GX

关于Python 日期时间与顺序问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55816282/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com