I have seen similar questions, my is little bit more practical, I would like to iterate over range of week days over and over again.
So far my iterator is not cyclic, help me please to resolve this.
def day_generator():
for w in ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']:
yield w;
g = day_generator()
print g.next()
You can use itertool's cycle: https://docs.python.org/2/library/itertools.html#itertools.cycle
import itertools
def day_generator():
days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
for day in itertools.cycle(days):
yield day
Long story short(and as mentioned in comments) it is really enough to make:
day_generator = itertools.cycle(days)
Thanks @FlavianHautbois
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With