0
Can someone explain this cycle?
2 Respostas
+ 1
itertools itself is not a cycle it's a library with many useful iteration tools. You can read about it here in Python course
+ 1
cycle lets you iterate through an iterable() infinitely.
I have created this sample program that illustrates it:
from itertools import cycle
test_list = [11,22,33,44,55]
test_string = "This is a test string"
j = 0
k = 0
for i in cycle(test_list):
j += 1
if(j > 10):
break
print(i)
for i in cycle(test_string):
k += 1
if(k> 42):
break
print(i)
now if you run the sample code in an IDE you can see the output is dependent on the value of the integer variables j & k.
These variable helps the sample code to terminate in finite steps.