python - Infinite cycle over a range starting at a particular number -


say have range:

r = range(1, 6) 

using range, want cycle infinitely , yield numbers come:

for in cycle(r):     yield(i) 

this correctly produce values of:

1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, ... 

however, want start yielding specific value continue on range it's defined. is, if want start @ 3, sequence be:

3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, ... 

is there way combination of range , cycle (or other way)?

just dropwhile until reach first value want emit:

>>> itertools import cycle, dropwhile >>> iterable = dropwhile(lambda x: x < 3, cycle(range(1, 6))) >>> _ in range(10): ...     print(next(iterable)) ... 3 4 5 1 2 3 4 5 1 2 

per docs (emphasis mine):

make iterator drops elements iterable long predicate true; afterwards, returns every element.

the predicate takes effect until first value evaluates false-y.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -