Reputation: 22021
Is there a python function which works well with day of year?
e.g. if I do range (200, 210)
it will work fine. However if I do range (350, 10)
, I want the values from 350 to 365 and then from 1 to 9. How do I do that?
I want a soln which works for both range(200, 210) and range (350, 10)
Upvotes: 0
Views: 48
Reputation: 59425
You can use modular arithmetic:
for i in range(350, 365+10):
print((i - 1) % 365 + 1)
That being said this is not the best way to implement this as not all years are 365 days.
Edit: You can also create your own generator:
def yearrange(start, stop):
if stop < start:
stop += 365
i = start
while i < stop:
yield (i - 1) % 365 + 1
i += 1
then you can simply do:
>>> print(list(yearrange(200, 210)))
[200, 201, 202, 203, 204, 205, 206, 207, 208, 209]
>>> print(list(yearrange(350, 10)))
[350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Upvotes: 3