避免嵌套for循环 [英] Avoiding nested for loops
问题描述
我正在尝试使用python在另一个代码上进行一些参数测试。我需要测试6个独立的参数,但我需要所有可能的组合。
每个参数都有一个最小值,一个最大值和一个需要传递给它的步长值。弹出到我头上的第一个简单的解决方案是一个嵌套的循环结构,看起来很可怕,像这样:
pre $ 对于xrange中的var1 (min1,max1,step1):
在xrange(min2,max2,step2)中为var2:
...
...
...
var6 in xrange(min6,max6,step6):
'''
在
进程中执行某些操作,因为处于六个嵌套for循环的中间
' ''
我决定,不!这不应该成立。所以我一直试图找出一种方法来递归地完成这个工作,或者至少是六次嵌套。我真的不能提出一个好的方案来这样做。对我来说最大的障碍是每个变量都有不同的最小值,最大值和步长值。
到目前为止,我的想法并不是很有帮助。我一直试图做一些递归函数的工作,但我不能想出它没有嵌套更多的功能循环。我已经在这里看到了很多对itertools.product的引用,但是我不太清楚怎么做。
编辑:我们正在创建一个以参数组合命名的目录,用这些参数编写一个文件,用这个文件运行另一个代码,然后分析这个代码的输出。除了最后一个循环之外,我没有做任何for循环。很多参数只有2个值,有的有10个,有的有3个,只是有所不同。
>以下是如何使用 product :
x1 = xrange(min1, max1,step1)
x2 = xrange(min2,max2,step2)
x3 = xrange(min3,max3,step3)
...
for v1, v2,v3,v4,v5,v6在itertools.product(x1,x2,x3,x4,x5,x6)中:
icky_thing(....)
/ pre>
或者更简洁些:
ranges =
xrange(min1,max1,step1),
xrange(min2,max2,step2),
xrange(min3,max3,step3),
...
]
for itertools.product(* ranges)中的v1,v2,v3,v4,v5,v6:
icky_thing(....)
I am trying to do some parameter testing on another code using python. I need to test 6 independent parameters, but I need all of the possible combinations of them.
Each parameter has a minimum, a maximum, and a step value that needs to be passed to it. The first easy solution that popped into my head was a nested for loop structure that looked horrific, like this:
for var1 in xrange(min1,max1,step1):
for var2 in xrange(min2,max2,step2):
...
...
...
for var6 in xrange(min6,max6,step6):
'''
Do something and be icky in the process due
to being in the middle of six nested for loops
'''
I decided, no! This shall not stand. So I've been trying to work out a way to do this recursively, or at the very least, not nested six times. I can't really come up with a good scheme to do so. The biggest obstacle for me is that each variable has a different min, max, and step value.
My thoughts so far aren't very helpful. I keep trying to make some recursive function work, but I just can't figure it out without nesting more for loops within the function. I've seen a lot of reference to itertools.product on here, but I can't quite figure out how to make that work either.
Edit: What I'm doing is creating a directory named after the combination of the parameters, writing a file with those parameters, running another code with this file, and analyzing the output from that code. I am doing nothing in literally any of the for loops except for the final one. A lot of the parameters only have 2 values, some have 10, others have 3... it just kind of varies.
解决方案 Here's how to use product
:
x1 = xrange(min1,max1,step1)
x2 = xrange(min2,max2,step2)
x3 = xrange(min3,max3,step3)
...
for v1, v2, v3, v4, v5, v6 in itertools.product(x1, x2, x3, x4, x5, x6):
icky_thing(....)
or a bit more compactly:
ranges = [
xrange(min1,max1,step1),
xrange(min2,max2,step2),
xrange(min3,max3,step3),
...
]
for v1, v2, v3, v4, v5, v6 in itertools.product(*ranges):
icky_thing(....)
这篇关于避免嵌套for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!