gpt4 book ai didi

python - 日期范围 : is there a more pythonic way? 中的所有星期二和星期三

转载 作者:太空狗 更新时间:2023-10-29 22:07:44 27 4
gpt4 key购买 nike

我想找到 2015-11-02 和 2015-12-14 之间的所有星期二和星期三(作为 datetime 对象)。这有效:

from datetime import datetime, timedelta

l = []
for i in range(100):
d = datetime(2015,11,2) + timedelta(days=i)
if d > datetime(2015,12,14):
break
if d.weekday() == 1 or d.weekday() == 2: # tuesday or wednesday
l.append(d)
print l

[datetime.datetime(2015, 11, 3, 0, 0), datetime.datetime(2015, 11, 4, 0, 0), datetime.datetime(2015, 11, 10, 0, 0), datetime.datetime(2015, 11, 11, 0, 0), datetime.datetime(2015, 11, 17, 0, 0), datetime.datetime(2015, 11, 18, 0, 0), datetime.datetime(2015, 11, 24, 0, 0), datetime.datetime(2015, 11, 25, 0, 0), datetime.datetime(2015, 12, 1, 0, 0), datetime.datetime(2015, 12, 2, 0, 0), datetime.datetime(2015, 12, 8, 0, 0), datetime.datetime(2015, 12, 9, 0, 0)]

是否有更 pythonic 的方法来做到这一点?

最佳答案

from datetime import datetime, timedelta

start, end = datetime(2015, 11, 2), datetime(2015, 12, 14)
days = (start + timedelta(days=i) for i in range((end - start).days + 1))
l = [d for d in days if d.weekday() in [1,2] ]

如果你跨越很长的跨度,这会快得多:

def helper(d, i, inc):
while d.weekday() != i:
d += timedelta(days=inc)
return d.replace(hour=0, minute=0, second=0, microsecond=0)


start, end = datetime(2015, 11, 02), datetime(2015,12, 14)


def find_days(st, end, d1, d2):
if st >= end:
raise ValueError("Start must be before end")
else:
_st, _end = helper(st, d1, inc=-1), helper(end, d2, 1)
secs = (_end - _st).total_seconds() // 86400
if st.weekday() == d2:
yield st
for i in range(int(secs / 7) + 1):
if st <= _st <= end:
yield _st
nxt = _st + timedelta(days=1)
if nxt <= end:
yield nxt
_st += timedelta(days=7)
if _st <= end:
yield _st
from pprint import pprint as pp



from pprint import pprint as pp


pp(list(find_days(start, end, 1, 2)))

输出:

[datetime.datetime(2015, 11, 3, 0, 0),
datetime.datetime(2015, 11, 4, 0, 0),
datetime.datetime(2015, 11, 10, 0, 0),
datetime.datetime(2015, 11, 11, 0, 0),
datetime.datetime(2015, 11, 17, 0, 0),
datetime.datetime(2015, 11, 18, 0, 0),
datetime.datetime(2015, 11, 24, 0, 0),
datetime.datetime(2015, 11, 25, 0, 0),
datetime.datetime(2015, 12, 1, 0, 0),
datetime.datetime(2015, 12, 2, 0, 0),
datetime.datetime(2015, 12, 8, 0, 0),
datetime.datetime(2015, 12, 9, 0, 0)]

这和 dateutil 做的一样,而且速度更快:

In [12]: def dte():
....: results = rrule(DAILY,
....: dtstart = dt.datetime(2015,11, 2),
....: until = end,
....: byweekday=(TU, WE),
....: )
....: return list(results)
....:


In [38]: start, end = datetime(2015, 11, 2), datetime(2100, 11, 14)

In [39]: for i in range(600):
end += timedelta(days=1)
assert dte() == list(find_days(start, end,1,2 ))
....:

In [40]: start, end = datetime(2015, 11, 2), datetime(2017, 11, 14)


In [41]: timeit [d for d in date_range(start, end) if d.weekday() in (1, 2)]
10 loops, best of 3: 62.1 ms per loop

In [42]: timeit list(find_days(start, end, 1, 2))
100 loops, best of 3: 8.11 ms per loop

In [43]: timeit dte()
10 loops, best of 3: 131 ms per loop

关于python - 日期范围 : is there a more pythonic way? 中的所有星期二和星期三,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34382737/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com