首页 > 解决方案 > 如何使用递归动态添加 n 嵌套 for 循环:

问题描述

我想动态添加 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}")

输出将是按排序顺序排列的总共 4 个数字(1、2、3、4)中的 3 个数字的所有组合,结果总数为:

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

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

我会喜欢类似的东西Total choose N elements(其中 N 等于该模式中嵌套 for 循环的数量),而我发现这样做的唯一方法是使用以前的模式对 N 嵌套 for 循环进行硬编码。

我已经尝试自己通过一些类似的问题来实现这一点stackoverflow,但是我没有成功地使用递归和希望的模式创建 for 循环,并且itertools combinations如果我想要类似的东西就不起作用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 循环的数量来获得它会很好。*感谢人们的反馈

标签: python-3.xloopsfor-looprecursioncombinatorics

解决方案


itertools.combinations方法对我来说很好。如果您在具有少量内存的虚拟机/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}")

推荐阅读