Let's say I have two iterables, one finite and one infinite:
import itertools
teams = ['A', 'B', 'C']
steps = itertools.count(0, 100)
I was wondering if I can avoid the nested for loop and use one of the infinite iterators from the itertools
module like cycle
or repeat
to get the Cartesian product of these iterables.
The loop should be infinite because the stop value for steps
is unknown upfront.
Expected output:
$ python3 test.py
A 0
B 0
C 0
A 100
B 100
C 100
A 200
B 200
C 200
etc...
Working code with nested loops:
from itertools import count, cycle, repeat
STEP = 100
LIMIT = 500
TEAMS = ['A', 'B', 'C']
def test01():
for step in count(0, STEP):
for team in TEAMS:
print(team, step)
if step >= LIMIT: # Limit for testing
break
test01()