V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
svsky
V2EX  ›  Python

Python 生成一个每隔 5 分钟的时间序列,能简化下吗?

  •  
  •   svsky · 2017-07-09 09:35:29 +08:00 · 7833 次点击
    这是一个创建于 2483 天前的主题,其中的信息可能已经有所发展或是发生改变。
    如下:
    for x in range(0,24):
    if len(str(x)) == 1:
    x = str(0)+str(x)
    for y in range(0,60,5):
    if len(str(y)) == 1:
    y = str(0)+str(y)
    print(str(x)+":"+str(y))
    14 条回复    2017-07-18 11:52:28 +08:00
    GTim
        1
    GTim  
       2017-07-09 09:48:04 +08:00
    ```python
    s = ["{:0>2}:{:0>2}".format(i/60,i%60) for i in range(0,86400,5)]
    ```
    GTim
        2
    GTim  
       2017-07-09 09:49:52 +08:00
    @GTim 把 5 分钟看成秒了,把 86400 换成 1440
    hxsf
        3
    hxsf  
       2017-07-09 09:50:51 +08:00
    [str(x) + ':' + str(y) for x in range(0, 24) for y in range(0, 60, 5)] 用推导式啊。。。
    hsmocc
        4
    hsmocc  
       2017-07-09 10:21:44 +08:00 via iPhone   ❤️ 1
    ["%02d:%02d" %(h,m) for h in range(0, 24) for m in range(0, 60, 5)]
    staticor
        5
    staticor  
       2017-07-09 11:09:32 +08:00
    如果用过 Pandas 的话有方便的时间序列函数.

    ```python
    import pandas as pd
    pd.date_range('2017-01-01 01:00:00', '2017-01-01 02:00:00', freq= '5min')

    ```
    Hieast
        6
    Hieast  
       2017-07-09 11:11:17 +08:00 via Android
    arrow 也有专门的时间序列函数
    hand515
        7
    hand515  
       2017-07-09 13:50:15 +08:00
    if len(str(y)) == 1:
    为啥不直接 if y< 10: 呢。
    raiz
        8
    raiz  
       2017-07-09 16:42:10 +08:00
    seq = ["%02d:%02d" %(i, j) for i in range(0,24) for j in range(0,60,5)]
    hugo775128583
        9
    hugo775128583  
       2017-07-09 16:44:02 +08:00 via Android
    你需要 rrule
    mckelvin
        10
    mckelvin  
       2017-07-10 09:17:45 +08:00
    ```
    import datetime
    start_dt = datetime.datetime(2017, 1, 1)
    interval = datetime.timedelta(seconds=300)
    for i in range(24 * 12):
    print (start_dt + interval * i).strftime("%H:%M")
    ```
    svsky
        11
    svsky  
    OP
       2017-07-10 09:46:24 +08:00
    @hsmocc 谢谢
    hwsdien
        12
    hwsdien  
       2017-07-10 11:37:43 +08:00
    import arrow
    start, end = arrow.now().span('day')
    print [repr(r) for r in arrow.Arrow.range('minute', start, end) if r.minute % 5 == 0]
    xuzixx1001
        13
    xuzixx1001  
       2017-07-10 16:00:59 +08:00
    9 楼+1 dateutil rrule
    yylucifer
        14
    yylucifer  
       2017-07-18 11:52:28 +08:00
    @GTim 这个不错
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5244 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 09:28 · PVG 17:28 · LAX 02:28 · JFK 05:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.