Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.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 生成两次之间5分钟间隔的列表_Python_Timedelta - Fatal编程技术网

Python 生成两次之间5分钟间隔的列表

Python 生成两次之间5分钟间隔的列表,python,timedelta,Python,Timedelta,我有以下字符串: start = "07:00:00" end = "17:00:00" 如何生成这些时间间隔为5分钟的列表,即 ["07:00:00","07:05:00",...,"16:55:00","17:00:00"] 这对我来说很有用,我相信你可以想出如何将结果放入列表中,而不是打印出来: >>> import datetime >>> start = "07:00:00" >>> end = "17:00:00" >&

我有以下字符串:

start = "07:00:00"
end = "17:00:00"
如何生成这些时间间隔为5分钟的列表,即

["07:00:00","07:05:00",...,"16:55:00","17:00:00"]

这对我来说很有用,我相信你可以想出如何将结果放入列表中,而不是打印出来:

>>> import datetime
>>> start = "07:00:00"
>>> end = "17:00:00"
>>> delta = datetime.timedelta(minutes=5)
>>> start = datetime.datetime.strptime( start, '%H:%M:%S' )
>>> end = datetime.datetime.strptime( end, '%H:%M:%S' )
>>> t = start
>>> while t <= end :
...     print datetime.datetime.strftime( t, '%H:%M:%S')
...     t += delta
... 
07:00:00
07:05:00
07:10:00
07:15:00
07:20:00
07:25:00
07:30:00
07:35:00
07:40:00
07:45:00
07:50:00
07:55:00
08:00:00
08:05:00
08:10:00
08:15:00
08:20:00
08:25:00
08:30:00
08:35:00
08:40:00
08:45:00
08:50:00
08:55:00
09:00:00
09:05:00
09:10:00
09:15:00
09:20:00
09:25:00
09:30:00
09:35:00
09:40:00
09:45:00
09:50:00
09:55:00
10:00:00
10:05:00
10:10:00
10:15:00
10:20:00
10:25:00
10:30:00
10:35:00
10:40:00
10:45:00
10:50:00
10:55:00
11:00:00
11:05:00
11:10:00
11:15:00
11:20:00
11:25:00
11:30:00
11:35:00
11:40:00
11:45:00
11:50:00
11:55:00
12:00:00
12:05:00
12:10:00
12:15:00
12:20:00
12:25:00
12:30:00
12:35:00
12:40:00
12:45:00
12:50:00
12:55:00
13:00:00
13:05:00
13:10:00
13:15:00
13:20:00
13:25:00
13:30:00
13:35:00
13:40:00
13:45:00
13:50:00
13:55:00
14:00:00
14:05:00
14:10:00
14:15:00
14:20:00
14:25:00
14:30:00
14:35:00
14:40:00
14:45:00
14:50:00
14:55:00
15:00:00
15:05:00
15:10:00
15:15:00
15:20:00
15:25:00
15:30:00
15:35:00
15:40:00
15:45:00
15:50:00
15:55:00
16:00:00
16:05:00
16:10:00
16:15:00
16:20:00
16:25:00
16:30:00
16:35:00
16:40:00
16:45:00
16:50:00
16:55:00
17:00:00
>>>
导入日期时间 >>>start=“07:00:00” >>>end=“17:00:00” >>>delta=datetime.timedelta(分钟=5) >>>start=datetime.datetime.strtime(开始,“%H:%M:%S”) >>>end=datetime.datetime.strtime(end,“%H:%M:%S”) >>>t=开始 >>>而t>>
这对我来说很有用,我相信你可以想出如何将结果放入列表中,而不是打印出来:

>>> import datetime
>>> start = "07:00:00"
>>> end = "17:00:00"
>>> delta = datetime.timedelta(minutes=5)
>>> start = datetime.datetime.strptime( start, '%H:%M:%S' )
>>> end = datetime.datetime.strptime( end, '%H:%M:%S' )
>>> t = start
>>> while t <= end :
...     print datetime.datetime.strftime( t, '%H:%M:%S')
...     t += delta
... 
07:00:00
07:05:00
07:10:00
07:15:00
07:20:00
07:25:00
07:30:00
07:35:00
07:40:00
07:45:00
07:50:00
07:55:00
08:00:00
08:05:00
08:10:00
08:15:00
08:20:00
08:25:00
08:30:00
08:35:00
08:40:00
08:45:00
08:50:00
08:55:00
09:00:00
09:05:00
09:10:00
09:15:00
09:20:00
09:25:00
09:30:00
09:35:00
09:40:00
09:45:00
09:50:00
09:55:00
10:00:00
10:05:00
10:10:00
10:15:00
10:20:00
10:25:00
10:30:00
10:35:00
10:40:00
10:45:00
10:50:00
10:55:00
11:00:00
11:05:00
11:10:00
11:15:00
11:20:00
11:25:00
11:30:00
11:35:00
11:40:00
11:45:00
11:50:00
11:55:00
12:00:00
12:05:00
12:10:00
12:15:00
12:20:00
12:25:00
12:30:00
12:35:00
12:40:00
12:45:00
12:50:00
12:55:00
13:00:00
13:05:00
13:10:00
13:15:00
13:20:00
13:25:00
13:30:00
13:35:00
13:40:00
13:45:00
13:50:00
13:55:00
14:00:00
14:05:00
14:10:00
14:15:00
14:20:00
14:25:00
14:30:00
14:35:00
14:40:00
14:45:00
14:50:00
14:55:00
15:00:00
15:05:00
15:10:00
15:15:00
15:20:00
15:25:00
15:30:00
15:35:00
15:40:00
15:45:00
15:50:00
15:55:00
16:00:00
16:05:00
16:10:00
16:15:00
16:20:00
16:25:00
16:30:00
16:35:00
16:40:00
16:45:00
16:50:00
16:55:00
17:00:00
>>>
导入日期时间 >>>start=“07:00:00” >>>end=“17:00:00” >>>delta=datetime.timedelta(分钟=5) >>>start=datetime.datetime.strtime(开始,“%H:%M:%S”) >>>end=datetime.datetime.strtime(end,“%H:%M:%S”) >>>t=开始 >>>而t>> 试试看:

# import modules
from datetime import datetime, timedelta

# Create starting and end datetime object from string
start = datetime.strptime("07:00:00", "%H:%M:%S")
end = datetime.strptime("17:00:00", "%H:%M:%S")

# min_gap
min_gap = 5

# compute datetime interval
arr = [(start + timedelta(hours=min_gap*i/60)).strftime("%H:%M:%S")
       for i in range(int((end-start).total_seconds() / 60.0 / min_gap))]
print(arr)
# ['07:00:00', '07:05:00', '07:10:00', '07:15:00', '07:20:00', '07:25:00', '07:30:00', ..., '16:55:00']
解释

# import modules
from datetime import datetime, timedelta

# Create starting and end datetime object from string
start = datetime.strptime("07:00:00", "%H:%M:%S")
end = datetime.strptime("17:00:00", "%H:%M:%S")

# min_gap
min_gap = 5

# compute datetime interval
arr = [(start + timedelta(hours=min_gap*i/60)).strftime("%H:%M:%S")
       for i in range(int((end-start).total_seconds() / 60.0 / min_gap))]
print(arr)
# ['07:00:00', '07:05:00', '07:10:00', '07:15:00', '07:20:00', '07:25:00', '07:30:00', ..., '16:55:00']
  • 首先,您需要将
    string
    date转换为datetime对象。这是谁干的
  • 然后,我们将找到开始日期和结束日期时间之间的分钟数。这就解决了!我们可以这样做:
  • 然而,在我们的例子中,我们只希望每
    n
    分钟迭代一次。因此,在我们的
    循环中
    ,我们需要将它除以
    n

    此外,由于我们将迭代这段时间,我们需要将
    for
    循环的
    int
    转换为
    。这导致:

    int((end-start).total_seconds() / 60.0 / min_gap)
    
  • 然后,在循环的每个元素上,我们将向初始日期时间添加分钟数。该功能是为以下目的而设计的。作为参数,我们指定要添加的小时数:
    minu gap*i/60

  • 最后,我们使用

  • 试试看

    # import modules
    from datetime import datetime, timedelta
    
    # Create starting and end datetime object from string
    start = datetime.strptime("07:00:00", "%H:%M:%S")
    end = datetime.strptime("17:00:00", "%H:%M:%S")
    
    # min_gap
    min_gap = 5
    
    # compute datetime interval
    arr = [(start + timedelta(hours=min_gap*i/60)).strftime("%H:%M:%S")
           for i in range(int((end-start).total_seconds() / 60.0 / min_gap))]
    print(arr)
    # ['07:00:00', '07:05:00', '07:10:00', '07:15:00', '07:20:00', '07:25:00', '07:30:00', ..., '16:55:00']
    
    解释

    # import modules
    from datetime import datetime, timedelta
    
    # Create starting and end datetime object from string
    start = datetime.strptime("07:00:00", "%H:%M:%S")
    end = datetime.strptime("17:00:00", "%H:%M:%S")
    
    # min_gap
    min_gap = 5
    
    # compute datetime interval
    arr = [(start + timedelta(hours=min_gap*i/60)).strftime("%H:%M:%S")
           for i in range(int((end-start).total_seconds() / 60.0 / min_gap))]
    print(arr)
    # ['07:00:00', '07:05:00', '07:10:00', '07:15:00', '07:20:00', '07:25:00', '07:30:00', ..., '16:55:00']
    
  • 首先,您需要将
    string
    date转换为datetime对象。这是谁干的
  • 然后,我们将找到开始日期和结束日期时间之间的分钟数。这就解决了!我们可以这样做:
  • 然而,在我们的例子中,我们只希望每
    n
    分钟迭代一次。因此,在我们的
    循环中
    ,我们需要将它除以
    n

    此外,由于我们将迭代这段时间,我们需要将
    for
    循环的
    int
    转换为
    。这导致:

    int((end-start).total_seconds() / 60.0 / min_gap)
    
  • 然后,在循环的每个元素上,我们将向初始日期时间添加分钟数。该功能是为以下目的而设计的。作为参数,我们指定要添加的小时数:
    minu gap*i/60

  • 最后,我们使用


  • start
    end
    是字符串。你打算以某种方式解析它们吗?
    start
    end
    是字符串。你会以某种方式解析它们吗?