我如何使用递归动态添加 n 嵌套 for 循环:

How can i add n nested for loops dynamically using recursion on this:

我想动态添加 N 个嵌套 for 循环,如果可能的话使用递归,在此代码中遵循以下模式:

 Total = 4
 counter = 0
 for i in range(1, Total + 1):
     for j in range(i + 1, Total + 1):
         for k in range(j + 1, Total + 1):
             print(i, j ,k)
             counter += 1
 print(f"Number of combinations: {counter}")

输出的是3个数共4个数(1,2,3,4)的所有组合,排序后的结果总数:

1 2 3
1 2 4
1 3 4
2 3 4
Number of combinations: 4

这是我的动机,我想动态增加组合的长度。

我喜欢 Total choose N elements(其中 N 等于在该模式中嵌套 for 循环),我发现这样做的唯一方法是使用先前的模式对 N 嵌套 for 循环进行硬编码。

我已经尝试通过在 Whosebug 上提出的一些类似问题来自己实现这个,但是我没有成功地使用递归、希望的模式和 itertools combinations 制作 for 循环如果我想要像 a combination of 15 elements(set of length 15) from 25 in total 这样的东西,请不要工作,如下所示:

#Adapted from https://www.geeksforgeeks.org/permutation-and-combination-in-python/
# A Python program to print all
# combinations of given length
from itertools import combinations
Total = 25
n = 15
counter = 0
# Get all combinations of [1,2,3..25]
# and length n
comb = combinations([i for i in range(1, Total + 1)], n)

# Print the obtained combinations
for i in list(comb):
    print(i)
    counter += 1
print(f"Number of combinations: {counter}")

预期输出:

               1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
               1 2 3 4 5 6 7 8 9 10 11 12 13 14 16
               1 2 3 4 5 6 7 8 9 10 11 12 13 14 17
                              ...
               10 12 13 14 15 16 17 18 19 20 21 22 23 24 25
               11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
               Number of combinations:  3.268.760

实际输出是:Killed,但如果像下面这样硬编码,它工作得很好并给出所需的输出:

def main():
    counter = 0
    for a in range (1, 26,1):
        for b in range (a + 1, 26,1):
            for c in range (b + 1, 26,1):
                for d in range (c + 1, 26,1):
                    for e in range (d + 1, 26,1):
                        for f in range (e + 1, 26,1):
                            for g in range (f + 1, 26,1):
                                for h in range (g + 1, 26,1):
                                    for i in range (h + 1, 26,1):
                                        for j in range (i + 1, 26,1):
                                            for k in range (j + 1, 26,1):
                                                for l in range (k + 1, 26,1):
                                                    for m in range (l + 1, 26,1):
                                                        for n in range (m + 1, 26,1):
                                                            for o in range (n + 1, 26,1):
                                                                print(a, b, c, d, e, f, g, h, i, j, k, l, m , n, o)
                                                                counter += 1
    print(f"Number of combinations: {counter}")
main()

但是,我无法根据需要对组合集进行增减。通过动态增加和减少嵌套 for 循环的数量来获得它会很好。 *感谢大家的反馈

itertools.combinations 方法对我来说效果很好。如果您 运行 在具有少量内存的虚拟 machine/docker 容器上,您可能会 运行 遇到内存问题。 Python 整数占用 24 个字节,因此您至少需要 74.8MB (24 * 3268760) 内存,加上列表开销(大约 25MB)来存储列表。

请注意,list(comb) 将尝试提前计算所有组合,并且会消耗内存。您一直在阅读的教程是错误的。您应该直接使用迭代器,即

...
for i in comb:
    print(i)
    counter += 1
print(f"Number of combinations: {counter}")

如果你只是追求项目的数量,你可以使用这个技巧来缩短它:

counter = sum(1 for item in comb)
print(f"Number of combinations: {counter}")