在Python中生成所有可能的长度为N的列表,这些列表总和为S.

Nic*_*253 4 python recursion sum list

我正在尝试生成所有可能的长度N的列表,总和为S.我已经编写了一些代码来执行此操作,但是在任何大的(特别是,我希望N = 5,S = 100),我遇到了内存溢出错误.

我正在寻找一个更好的问题解决方案,或者一种改进我的代码的方法,以便我可以在N = 5,S = 100上运行它.下面这两个程序协同工作,在嵌套列表中创建所有可能的数字组合,然后将它们重新设置为正确的格式.下面再现了一些样本输出.

我知道我的代码不是最好的.我是一名工程师(我知道,我知道),所以编码并不完全是我的专长.我感谢您提供的任何帮助.

编辑:我只是想澄清一些事情.首先,列表中的零可以,列表可以包含相同数字的倍数,列表中数字的顺序很重要.

def nToSum(N,S):
    ''' Creates a nested list of all possible lists of length N that sum to S'''
    if N <= 1: #base case
        return [S]
    else:
        L = []
        for x in range(S+1):   #create a sub-list for each possible entry of 0 to S 
            L += [[x,nToSum(N-1,S-x)]]  #create a sub-list for this value recursively
        return L

def compress(n=[],L): #designed to take in a list generated by nToSum
    '''takes the input from nToSum as list L, and then flattens it so that each list is a
       top level list.  Leading set n is the "prefix" list, and grows as you climb down the 
       sublists'''
    if type(L[0]) == int:  #base case:  you have exposed a pure integer
        return [n+L]       #take that integer, and prepend the leading set n
    else:
        Q = []
        for x in L:  # look at every sublist
            Q += compress(n+[x[0]],x[1])  # for each sublist, create top level lists recursively
        return Q                          # note:  append x[0] to leading set n

>>> nToSum(3,3)
[[0, [[0, [3]], [1, [2]], [2, [1]], [3, [0]]]], [1, [[0, [2]], [1, [1]], [2, [0]]]], [2, [[0, [1]], [1, [0]]]], [3, [[0, [0]]]]]

>>> compress([],nToSum(3,3))
[[0, 0, 3], [0, 1, 2], [0, 2, 1], [0, 3, 0], [1, 0, 2], [1, 1, 1], [1, 2, 0], [2, 0, 1], [2, 1, 0], [3, 0, 0]]
Run Code Online (Sandbox Code Playgroud)

Mar*_*nen 9

使用生成器节省内存(使用xrange而不是range使用Python 2).这就是我提出的.它非常类似于你nToSum的需要compress.

def sums(length, total_sum):
    if length == 1:
        yield (total_sum,)
    else:
        for value in range(total_sum + 1):
            for permutation in sums(length - 1, total_sum - value):
                yield (value,) + permutation

L = list(sums(5,100))
print('total permutations:',len(L))

# First and last 10 of list
for i in L[:10] + L[-10:]:
    print(i)
Run Code Online (Sandbox Code Playgroud)

产量

total permutations: 4598126
(0, 0, 0, 0, 100)
(0, 0, 0, 1, 99)
(0, 0, 0, 2, 98)
(0, 0, 0, 3, 97)
(0, 0, 0, 4, 96)
(0, 0, 0, 5, 95)
(0, 0, 0, 6, 94)
(0, 0, 0, 7, 93)
(0, 0, 0, 8, 92)
(0, 0, 0, 9, 91)
(98, 0, 2, 0, 0)
(98, 1, 0, 0, 1)
(98, 1, 0, 1, 0)
(98, 1, 1, 0, 0)
(98, 2, 0, 0, 0)
(99, 0, 0, 0, 1)
(99, 0, 0, 1, 0)
(99, 0, 1, 0, 0)
(99, 1, 0, 0, 0)
(100, 0, 0, 0, 0)
Run Code Online (Sandbox Code Playgroud)