使用递归嵌套 for 循环:如何访问外循环的索引

Nested for loop using recursivity: how to access the indices of the outer loops

我试图制作一个能够使用递归给出嵌套循环结果的函数,因为循环的数量可以改变。 (而且我不想手动制作每个嵌套循环)。

例如:

l1=[1, 2, 3, ...., 9] #create with np.arange()
l2=[0.1, 0.2, 0.3, ..., 0.9]
L=[l1,l2]

然后我定义递归循环

def loop_rec(L, k):    #k is the number of parameter
    for i in L[k]:
        if (k >=0):
            loop_rec(L,k-1)

想象一下,我想找到(参数 1 x 参数 2)的最大值

如果没有递归,我会:

for i in L[0]:
    for j in L[1]:
        m=i*j    #m is the multiplication of the two parameters
        if m>M:
            M=m    #M is the max value

但是通过递归循环,我没有“i”和“j”,我只有“i”。 而且,我不知道我应该把乘法和验证放在哪里...

如果我添加 "print(i)":

def loop_rec(L, k):
    for i in L[k]:
        if (k >=0):
            loop_rec(L,k-1)
            print(i, end=';')

我得到了:

0;1;2;3;4;5;6;7;8;9;0.1;0;1;2;3;4;5;6;7;8;9;0.2;0;1;2;3;4;5;6;7;8;9;0.3;0;1;2;3;4;5;6;7;8;9;0.4;0;1;2;3;4;5;6;7;8;9;0.5;0;1;2;3;4;5;6;7;8;9;0.6;0;1;2;3;4;5;6;7;8;9;0.7;0;1;2;3;4;5;6;7;8;9;0.8;0;1;2;3;4;5;6;7;8;9;0.9;None

所以它似乎有效,但如果你能帮助我计算以及如何获得每个值,你会很好:)

你可以使用这个:

def loop_rec(L, k, i_in_prev_rec=None):
    if i_in_prev_rec is None:              # First call (no recursion yet)
        i_in_prev_rec = []
    if (k >=0):
        print()
        for (i, elem) in enumerate(L[k]):
            # elem contains L[k][i]
            i_in_prev_rec.append(i)        # Append i, so the next recursion can see it
            loop_rec(L,k-1, i_in_prev_rec)
            i_in_prev_rec.pop()
    else:
        print(f"Reached innermost recursion/loop. The loop indices are: {i_in_prev_rec}")
        my_sum = sum([L[k][ind] for (k,ind) in enumerate(i_in_prev_rec)])
        print(f"The sum L0[ind0] + L[1][ind1] + L[2][ind2] + ... is: {my_sum}")

主调用:

loop_rec(L, len(L)-1)

输出:

Reached innermost recursion/loop. The loop indices are: [0, 0]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 0.0
Reached innermost recursion/loop. The loop indices are: [0, 1]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 0.1
Reached innermost recursion/loop. The loop indices are: [0, 2]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 0.2
Reached innermost recursion/loop. The loop indices are: [0, 3]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 0.3
Reached innermost recursion/loop. The loop indices are: [0, 4]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 0.4
Reached innermost recursion/loop. The loop indices are: [0, 5]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 0.5
Reached innermost recursion/loop. The loop indices are: [0, 6]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 0.6
Reached innermost recursion/loop. The loop indices are: [0, 7]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 0.7
Reached innermost recursion/loop. The loop indices are: [0, 8]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 0.8

Reached innermost recursion/loop. The loop indices are: [1, 0]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 1.0
Reached innermost recursion/loop. The loop indices are: [1, 1]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 1.1
Reached innermost recursion/loop. The loop indices are: [1, 2]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 1.2
Reached innermost recursion/loop. The loop indices are: [1, 3]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 1.3
Reached innermost recursion/loop. The loop indices are: [1, 4]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 1.4
Reached innermost recursion/loop. The loop indices are: [1, 5]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 1.5
Reached innermost recursion/loop. The loop indices are: [1, 6]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 1.6
Reached innermost recursion/loop. The loop indices are: [1, 7]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 1.7
Reached innermost recursion/loop. The loop indices are: [1, 8]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 1.8

Reached innermost recursion/loop. The loop indices are: [2, 0]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 2.0
Reached innermost recursion/loop. The loop indices are: [2, 1]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 2.1
Reached innermost recursion/loop. The loop indices are: [2, 2]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 2.2
Reached innermost recursion/loop. The loop indices are: [2, 3]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 2.3
Reached innermost recursion/loop. The loop indices are: [2, 4]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 2.4
Reached innermost recursion/loop. The loop indices are: [2, 5]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 2.5
Reached innermost recursion/loop. The loop indices are: [2, 6]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 2.6
Reached innermost recursion/loop. The loop indices are: [2, 7]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 2.7
Reached innermost recursion/loop. The loop indices are: [2, 8]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 2.8

Reached innermost recursion/loop. The loop indices are: [3, 0]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 3.0
Reached innermost recursion/loop. The loop indices are: [3, 1]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 3.1
Reached innermost recursion/loop. The loop indices are: [3, 2]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 3.2
Reached innermost recursion/loop. The loop indices are: [3, 3]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 3.3
Reached innermost recursion/loop. The loop indices are: [3, 4]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 3.4
Reached innermost recursion/loop. The loop indices are: [3, 5]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 3.5
Reached innermost recursion/loop. The loop indices are: [3, 6]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 3.6
Reached innermost recursion/loop. The loop indices are: [3, 7]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 3.7
Reached innermost recursion/loop. The loop indices are: [3, 8]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 3.8

Reached innermost recursion/loop. The loop indices are: [4, 0]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 4.0
Reached innermost recursion/loop. The loop indices are: [4, 1]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 4.1
Reached innermost recursion/loop. The loop indices are: [4, 2]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 4.2
Reached innermost recursion/loop. The loop indices are: [4, 3]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 4.3
Reached innermost recursion/loop. The loop indices are: [4, 4]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 4.4
Reached innermost recursion/loop. The loop indices are: [4, 5]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 4.5
Reached innermost recursion/loop. The loop indices are: [4, 6]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 4.6
Reached innermost recursion/loop. The loop indices are: [4, 7]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 4.7
Reached innermost recursion/loop. The loop indices are: [4, 8]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 4.8

Reached innermost recursion/loop. The loop indices are: [5, 0]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 5.0
Reached innermost recursion/loop. The loop indices are: [5, 1]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 5.1
Reached innermost recursion/loop. The loop indices are: [5, 2]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 5.2
Reached innermost recursion/loop. The loop indices are: [5, 3]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 5.3
Reached innermost recursion/loop. The loop indices are: [5, 4]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 5.4
Reached innermost recursion/loop. The loop indices are: [5, 5]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 5.5
Reached innermost recursion/loop. The loop indices are: [5, 6]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 5.6
Reached innermost recursion/loop. The loop indices are: [5, 7]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 5.7
Reached innermost recursion/loop. The loop indices are: [5, 8]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 5.8

Reached innermost recursion/loop. The loop indices are: [6, 0]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 6.0
Reached innermost recursion/loop. The loop indices are: [6, 1]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 6.1
Reached innermost recursion/loop. The loop indices are: [6, 2]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 6.2
Reached innermost recursion/loop. The loop indices are: [6, 3]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 6.3
Reached innermost recursion/loop. The loop indices are: [6, 4]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 6.4
Reached innermost recursion/loop. The loop indices are: [6, 5]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 6.5
Reached innermost recursion/loop. The loop indices are: [6, 6]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 6.6
Reached innermost recursion/loop. The loop indices are: [6, 7]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 6.7
Reached innermost recursion/loop. The loop indices are: [6, 8]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 6.8

Reached innermost recursion/loop. The loop indices are: [7, 0]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 7.0
Reached innermost recursion/loop. The loop indices are: [7, 1]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 7.1
Reached innermost recursion/loop. The loop indices are: [7, 2]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 7.2
Reached innermost recursion/loop. The loop indices are: [7, 3]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 7.3
Reached innermost recursion/loop. The loop indices are: [7, 4]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 7.4
Reached innermost recursion/loop. The loop indices are: [7, 5]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 7.5
Reached innermost recursion/loop. The loop indices are: [7, 6]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 7.6
Reached innermost recursion/loop. The loop indices are: [7, 7]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 7.7
Reached innermost recursion/loop. The loop indices are: [7, 8]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 7.8

Reached innermost recursion/loop. The loop indices are: [8, 0]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 8.0
Reached innermost recursion/loop. The loop indices are: [8, 1]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 8.1
Reached innermost recursion/loop. The loop indices are: [8, 2]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 8.2
Reached innermost recursion/loop. The loop indices are: [8, 3]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 8.3
Reached innermost recursion/loop. The loop indices are: [8, 4]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 8.4
Reached innermost recursion/loop. The loop indices are: [8, 5]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 8.5
Reached innermost recursion/loop. The loop indices are: [8, 6]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 8.6
Reached innermost recursion/loop. The loop indices are: [8, 7]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 8.7
Reached innermost recursion/loop. The loop indices are: [8, 8]
The sum L[0][ind0] + L[1][ind1] + L[2][ind2] + ... is: 8.8