Python:每小时迭代两次日期时间 [英] Python: iterate through two datetimes hourly

查看:91
本文介绍了Python:每小时迭代两次日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是问题:

使用python的datetime模块,创建一个迭代器,该迭代器生成两个datetime.datatime对象之间的所有小时。

Using python's datetime module, create an iterator that generates all the hours between two datetime.datatime objects.

it = hourly_it(d1,d2)#其中 d1 d2 datetime.datetime 对象。
例如,

it = hourly_it( d1, d2) # where d1 and d2 are datetime.datetime objects. For example,

from datetime import datetime
it = hourly_it( datetime(2018, 10, 2, 12), datetime(2018, 10, 3, 12))

将生成所有日期时间在11月3日中午12:00(至11月3日中午12:00)之间。

will generates all the date times between 12:00 (noon) Novermber 2, to 12:00 (noon) November 3rd.

这是我的代码:

from datetime import date, timedelta as td, datetime
   def hourly_it( d1, d2) :
      start = d1
      finish = d2
      def f():
         nonlocal start, finish
         if(start>=finish): return None
         start += td(hours=1)
         return start   
      return f

当我使用以下代码测试代码时:

when I test the code using:

from datetime import datetime
  it = hourly_it( datetime(2018, 10, 2, 12), datetime(2018, 10, 3,12))
  print(next(it))

我收到此错误:


TypeError追溯(最近一次通话
最后一次)

TypeError Traceback (most recent call last)

in()
1 from datetime import datetime
2 it = hourly_it(datetime(2018 ,10,2,12),datetime(2018,10,3,12))

in () 1 from datetime import datetime 2 it = hourly_it( datetime(2018, 10, 2, 12), datetime(2018, 10, 3, 12))

----> 3 print(next(it))

----> 3 print(next(it))

TypeError:'function'对象不是迭代器

TypeError: 'function' object is not an iterator

我知道了。谢谢大家这是不使用生成器的情况下的操作方法。我还应注意,我将+1小时放在返回值之前,并将起始时间> =

I figured it out. Thanks all. Here is how you do it without using the generator. I should also note that I put the +1hour before the return and set the start >=

from datetime import date, timedelta as td, datetime
  def hourly_it( d1, d2) :
     start = d1
     finish = d2
     def f():
          nonlocal start, finish
          if(start>=finish): return None
          start += td(hours=1)
          return start

      return iter(f,None)


推荐答案

我几乎确定这是学校的作业。

I'm almost sure this is school work you got there.

但是,我将为您提供有关python迭代器协议的一些阅读信息,并且简短易懂的示例也许可以理解;

But I'll give you some reading on python's iterator protocol, and short and sweet example maybe you get it; and its not rocket science anyway.

迭代器协议: For循环如何在Python中工作

>>> from datetime import datetime, timedelta
>>> def hourly_it(start, finish):
...     while finish > start:
...             start = start + timedelta(hours=1)
...             yield start
>>> start = datetime(2018, 10, 2, 12)
>>> finish = datetime(2018, 10, 3, 12)
>>> for hour in hourly_it(start, finish):
...     print(hour)
...
2018-10-02 13:00:00
2018-10-02 14:00:00
2018-10-02 15:00:00
2018-10-02 16:00:00
2018-10-02 17:00:00
2018-10-02 18:00:00
2018-10-02 19:00:00
2018-10-02 20:00:00
2018-10-02 21:00:00
2018-10-02 22:00:00
2018-10-02 23:00:00
2018-10-03 00:00:00
2018-10-03 01:00:00
2018-10-03 02:00:00
2018-10-03 03:00:00
2018-10-03 04:00:00
2018-10-03 05:00:00
2018-10-03 06:00:00
2018-10-03 07:00:00
2018-10-03 08:00:00
2018-10-03 09:00:00
2018-10-03 10:00:00
2018-10-03 11:00:00
2018-10-03 12:00:00

这篇关于Python:每小时迭代两次日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆