python - 使用 python for 循环创建时间列表

标签 python pandas numpy

我想创建一个格式为 00:00 到 23:45 的时间列表。我认为这可以使用 for 循环来完成,但我不确定该怎么做。只要使用正确的 00:00 格式,我就不关心列表中值的类型。现在我只是用设置值实例化列表,代码如下所示。感谢您的帮助!

''' python

times = ['00:00', '00:15' , '00:30' , '00:45' , 
        '01:00', '01:15' , '01:30' , '01:45' , 
        '02:00', '02:15' , '02:30' , '02:45' , 
        '03:00', '03:15' , '03:30' , '03:45' , 
        '04:00', '04:15' , '04:30' , '04:45' , 
        '05:00', '05:15' , '05:30' , '05:45' , 
        '06:00', '06:15' , '06:30' , '06:45' , 
        '07:00', '07:15' , '07:30' , '07:45' , 
        '08:00', '08:15' , '08:30' , '08:45' , 
        '09:00', '09:15' , '09:30' , '09:45' , 
        '10:00', '10:15' , '10:30' , '10:45' , 
        '11:00', '11:15' , '11:30' , '11:45' , 
        '12:00', '12:15' , '12:30' , '12:45' , 
        '13:00', '13:15' , '13:30' , '13:45' , 
        '14:00', '14:15' , '14:30' , '14:45' , 
        '15:00', '15:15' , '15:30' , '15:45' , 
        '16:00', '16:15' , '16:30' , '16:45' , 
        '17:00', '17:15' , '17:30' , '17:45' , 
        '18:00', '18:15' , '18:30' , '18:45' , 
        '19:00', '19:15' , '19:30' , '19:45' , 
        '20:00', '20:15' , '20:30' , '20:45' , 
        '21:00', '21:15' , '21:30' , '21:45' , 
        '22:00', '22:15' , '22:30' , '22:45' , 
        '23:00', '23:15' , '23:30' , '23:45' ]

'''

最佳答案

只使用纯 for 循环:

times = []
for hour in range(24):
    for minute in range(0, 60, 15):
        times.append('{:02d}:{:02d}'.format(hour, minute))

关于python - 使用 python for 循环创建时间列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58052066/

相关文章:

python - 使用 Python 将变量添加到现有的 Netcdf4 文件

Python正则表达式匹配子字符串

python-3.x - 如何根据同一数据框中的另一列计算 Pandas 数据框中的值

python - 有很多字典的列表 VS 有很少列表的字典?

numpy - 如何控制genfromtxt读取指定的行?

python - Numpy bool 掩码——多维和 float

python - 如果我在自己的python软件包中使用了numpy并分发了自己的软件包,那么我应该包括numpy许可证吗?

python - 类型错误 : the first argument must be callable in scheduler library

python - Pandas DataFrame 到包含标题的列表列表

python - 用于编辑 numpy 计数数组的一行解决方案? (Python)