python 小时循环
时间: 2023-09-14 12:05:21 浏览: 145
1小时学会Python
在Python中,可以使用以下代码实现小时循环:
```python
def hour_range(bgn, end):
fmt = '%Y/%m/%d %H:%M:%S'
begin = datetime.datetime.strptime(bgn, fmt)
end = datetime.datetime.strptime(end, fmt)
delta = datetime.timedelta(hours=1)
interval = int((end - begin).days * 24 + (end - begin).seconds / 3600) + 1
return [begin + delta * i for i in range(interval)]
```
这个函数接受两个参数 `bgn` 和 `end`,分别表示开始时间和结束时间。它将这两个时间字符串转换为 `datetime` 对象,并定义了每小时的时间间隔 `delta`。然后,通过计算时间间隔的个数,生成一个包含每小时时间点的列表,并返回该列表。请注意,我们将时间间隔的个数加1,以便包括结束时间。因此,使用这个函数可以得到指定时间范围内的每小时时间点的列表。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [【Python】时间循环 / 日期循环](https://blog.csdn.net/morcit333/article/details/105410698)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [python for循环优化](https://download.csdn.net/download/weixin_38526650/13754576)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [python进行数据增强](https://download.csdn.net/download/doyoboy/88278532)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文