Suppose I have a nested loop of the form:
for i in List1:
for j in List2:
DoSomething(i,j)
Can it be done as follows:
for i,j in combine(List1, List2):
DoSomething(i,j)
Thanks in advance
So to clarify the combine function would do something as follows:
List1 = range(5)
List2 = range(5)
combine(List1, List2,)
>>> (0,0)
>>> (0,1)
>>> (0,2)
.
.
.
>>> (2,4)
>>> (3,0)
.
.
.
The itertools.product works perfectly
解决方案
You can use itertools.product
import itertools
for i,j in itertools.product(List1, List2):
DoSomething(i,j)