Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 可能是嵌套的For循环 start\u月=10 开始日=24 月末=11 结束日=28 时间戳=[] 对于范围(0,24)内的r: 如果时间范围为0_Python - Fatal编程技术网

Python 可能是嵌套的For循环 start\u月=10 开始日=24 月末=11 结束日=28 时间戳=[] 对于范围(0,24)内的r: 如果时间范围为0

Python 可能是嵌套的For循环 start\u月=10 开始日=24 月末=11 结束日=28 时间戳=[] 对于范围(0,24)内的r: 如果时间范围为0,python,Python,,则需要函数。这是一种可行的方法: start_month = 10 start_day = 24 end_month = 11 end_day = 28 time_stamp = [] for r in range(0,24): if 0 <=r <= 9: hr_str = '0' + str(r) + ':' time_stamp.append(hr_str) else: hr_str = str(r)+ ':

,则需要函数。这是一种可行的方法:

start_month = 10
start_day = 24
end_month = 11
end_day = 28


time_stamp = []
for r in range(0,24):
    if 0 <=r <= 9:
        hr_str = '0' + str(r) + ':'
        time_stamp.append(hr_str)
    else:
        hr_str = str(r)+ ':'
        time_stamp.append(hr_str)
导入日期时间
开始月份=12
开始日=24
月末=1
结束日=28

start=datetime.datetime(2019,start_MOUNT,start_day)#除非您希望获得特定的输出,否则我看不出问题所在?现在,我只获得小时选项,我还需要天和月选项。例如,我希望第一个输出是“10-24 00”,然后是“10-24 01”。嗨,如果你在问题中包含你想要的输出样本,这对回答问题的人真的很有用。第二,你的问题标题实际上不是一个问题,但表明你可能已经知道问题的答案。是的,嵌套循环很可能是一种方式。我建议您尝试使用嵌套循环的解决方案,如果您仍然卡住或有特定问题,请更新您的问题。如果月份相同,您将如何使用此解决方案?这就是range()的工作方式。范围(10,10)->无范围(10,11)->10。因此,如果将1添加到范围,它将起作用。看我修改过的答案。对不起,我应该亲眼看到的。谢谢你修改。这是一种工作,除了当它切换时,我需要它从第31天转到第11个月的第1天。这是另一种类型的问题。要处理真实日期,您需要datetime函数。您还需要指定一年以适应可能的闰年。我又一次改变了答案。这正是我所需要的!非常感谢你!!!
import datetime

start_month = 12
start_day = 24
end_month = 1
end_day = 28

start = datetime.datetime(2019, start_month, start_day)  # <- 2019 specified
end = datetime.datetime(2020, end_month, end_day)        # <- 2020 specified

lst = [(start + datetime.timedelta(hours=h)).strftime('%m-%d %H') for h in range((end-start).days * 24)]

for datehour in lst:
    print(datehour)