I want to yield through 2 different itertools.count. I have combined the two
generators using itertools.chain.from_iterable
This is the code I have written for it.
return itertools.chain.from_iterable([itertools.count(start=2, step=2), itertools.count(start=7, step=7)])
The problem is that it is trying to finish the first counter (step 2) before proccding to yield over next counter (step 7)
Output from the above sample code:
2
4
6
8
10
...
But I want to cycle over alternatively.
Expected Output:
2 # 2*1
7 # 7*1
4 # 2*2
14 # 7*2
6 # 2*3
21 # 7*3
8 # 2*4
28 # 7*4
Here are the other ways I have tried so far:
yield from [elem for elem in [next(count(start=2, step=2)), next(count(start=7, step=7))]]
The above cycles alternatively but the counter resets after each yield.
Output from the above code sample:
2
7
2
7
2
7
I want this to be implemented entirely on itertools or list comprehension
since they are memory optimized, hence I expect the function to return a generator object. Also, it would be better if the solution is on a single line.
EDIT:
As suggested by jonrsharpe in the comment, I have implemented roundrobin iter technique and I am able to fetch the desired output.
from itertools import count, cycle
def pattern_generator():
return cycle(iter(it).__next__ for it in [
count(start=2, step=2),
count(start=7, step=7),
])
gen = pattern_generator()
print(next(gen)())
print(next(gen)())
print(next(gen)())
print(next(gen)())
I am satisfied with this output. But, is it possible to call next without calling the iter's next method? i.e., without using () in next(gen)()?
Thanks in advance.