V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
csdreamdong
V2EX  ›  Python

Python 中 xrange 生成的最后个项怎么判断。

  •  
  •   csdreamdong · Jul 17, 2017 · 2949 views
    This topic created in 3207 days ago, the information mentioned may be changed or developed.
    start_time = '2017-01-01 00:00:00'
    end_time = '2017-07-01 00:00:00'
    start_time_st = int(time.mktime(time.strptime(start_time, '%Y-%m-%d %H:%M:%S')))
    end_time_st = int(time.mktime(time.strptime(end_time, '%Y-%m-%d %H:%M:%S')))
    
    for stmp in xrange(start_time_st, end_time_st, 60):
        ......
        需要生成一个 list,
        if len(list)>=100 or stmp 是最后一个时:
            do.....
    

    这个场景下,怎么判断 stmp 已经是最后一个了。求助大神们。

    11 replies    2017-07-18 08:46:20 +08:00
    csdreamdong
        1
    csdreamdong  
    OP
       Jul 17, 2017
    python2.7
    sleshep
        2
    sleshep  
       Jul 17, 2017
    用 arrow 库啊,你这么折腾时间不难受啊
    sleshep
        3
    sleshep  
       Jul 17, 2017
    import arrow
    arrow.get(2017).span('month')[:7]


    2 行搞定的事情
    csdreamdong
        4
    csdreamdong  
    OP
       Jul 17, 2017
    @sleshep 我想指定一段时间,每隔 60 秒一个时间戳。要这段时间内的所有时间戳。
    sleshep
        5
    sleshep  
       Jul 17, 2017
    >>> from datetime import datetime
    >>> import arrow
    >>> start = datetime(2013, 5, 5, 12, 30)
    >>> end = datetime(2013, 5, 5, 17, 15)
    >>> for r in arrow.Arrow.range('hour', start, end):
    ... print repr(r)
    ...
    <Arrow [2013-05-05T12:30:00+00:00]>
    <Arrow [2013-05-05T13:30:00+00:00]>
    <Arrow [2013-05-05T14:30:00+00:00]>
    <Arrow [2013-05-05T15:30:00+00:00]>
    <Arrow [2013-05-05T16:30:00+00:00]>
    csdreamdong
        6
    csdreamdong  
    OP
       Jul 17, 2017
    @sleshep 但怎么判断是不是最后个呢?
    rogerchen
        7
    rogerchen  
       Jul 17, 2017
    for i, stmp in enumerate(xrange(...))
    KingMaster
        8
    KingMaster  
       Jul 17, 2017
    if stmp + 60 >= end_time_st:
    ...
    KgM4gLtF0shViDH3
        9
    KgM4gLtF0shViDH3  
       Jul 17, 2017
    @rogerchen 这还不如直接用 range。。。
    crab
        10
    crab  
       Jul 17, 2017
    用 unix 时间,再转回去。
    zjuhwc
        11
    zjuhwc  
       Jul 18, 2017 via iPhone
    把是不是最后一个的判断挪到循环外面去?
    因为 for 循环结束后,stmp 就停在最后一个值上了,循环外做一次 do 操作
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1086 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 54ms · UTC 22:46 · PVG 06:46 · LAX 15:46 · JFK 18:46
    ♥ Do have faith in what you're doing.