假设我有一个如下形式的嵌套循环:
for i in List1:
for j in List2:
DoSomething(i,j)
是否可以按照以下方式进行:
for i,j in combine(List1, List2):
DoSomething(i,j)
提前致谢
因此,为了澄清 combine 函数将执行以下操作:
List1 = range(5)
List2 = range(5)
combine(List1, List2,)
>>> (0,0)
>>> (0,1)
>>> (0,2)
.
.
.
>>> (2,4)
>>> (3,0)
.
.
.
itertools.product 完美运行
您可以使用 itertools.product
import itertools
for i,j in itertools.product(List1, List2):
DoSomething(i,j)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句