I use itertools.product in its usual form to generate a list of tuples.
I then iterate through the list with a standard for i in myList:
loop to generate desired results.
I then wish to iterate a second time through the same loop using for i in myList:
, but it does not execute the second loop without me regenerating the myList
prior to loop entry.
Why is this ?
I have looked at the itertools help in the python docs which gives an explanation of what itertools.product() does, but it does not explain why the user would need to invoke before each loop entry.
https://docs.python.org/2/library/itertools.html#itertools.product
import itertools as it
def loopsThings(*lsts):
# list of possibilities
allPossible = it.product(a, b, c)
for i in allPossible:
print('first pass', i)
# list of possibilities
# second pass works if this is un-commented !!
# allPossible = it.product(a, b, c)
for i in allPossible:
print('second pass', i)
return
a = [3, 5, 10]
b = [1, 9, 15]
c = [4, 8]
loopsThings(a, b, c)
i expect to get 2 lists, but only get 1 list (first pass
)
in order to get the second list i need to run allPossible = it.product(a, b, c)
before entry of the second loop.