-->

如何使用嵌套来为暴力破解组合循环 - 的Python(How to use nested for l

2019-10-28 08:36发布

我要做到以下几点:1)尝试各种输入组合来搜索最佳效果2)重置所有阵列,因为他们的代码的每个循环之前

每个变量我正在与工作是在一个阵列,例如F [0,1,2,3,...]

这个问题可能会在各个通部分后复位变量,第一遍工作正常,但第一遍的残留物会导致以下迭代早破..

这里是我的方法伪。 所以很简单,有可能与Python如何处理数据的问题(面向对象)..

index_save = index
for input1 in [0.1,0.2,0.3,...]
    for input2 in [10,20,30,...]
        for input3 in [-0.1,-0.2,-0.3,...]
            index = index_save  #To reset the index and thus all arrays
            while True:
                index = index + 1
                f[index] = *Function of inputs*
                result = *Function of f and inputs*
                if condition_met = true
                    break
            if result > result_best
                result_best = result
                inputs_best = [input1,input2,input3]

Answer 1:

原来,回答我的问题如下。

使用嵌套的for循环来强力组合工程(显然)。 使用我的问题概括的方法的工作这样做。 需要护理的一部分,是确保您已成功重置每遍所有变量 。 这意味着,在整数将不得不手动复位变量 。 这是相反的,我怎么能简单地通过重新索引重置所有阵列。

integer_save = integer
index_save = index
for input in range
    index = index_save
    integer = integer_save
    index = index + 1
    array[index] = (physics functions based on input)
    integer = (physics functions based on input)


文章来源: How to use nested for loops for brute forcing combinations - Python