Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python列表中总和==n的所有可能的数字集_Python_List - Fatal编程技术网

python列表中总和==n的所有可能的数字集

python列表中总和==n的所有可能的数字集,python,list,Python,List,我想根据n对列表做一些更改。例如: l=[0,0,0,0,0,0,0,0,0,0,0]n=4: print l #first iteration will print :[1,1,1,1,0,0,0,0,0,0,0,0] # 4=1+1+1+1 print l #next iteration will print :[1,1,0,2,0,0,0,0,0,0,0,0] # 4=1+1+2 print l #next iteration will print

我想根据n对列表做一些更改。例如:

l=[0,0,0,0,0,0,0,0,0,0,0]n=4:

 print   l   #first iteration will print :[1,1,1,1,0,0,0,0,0,0,0,0]    # 4=1+1+1+1
 print   l   #next iteration will print :[1,1,0,2,0,0,0,0,0,0,0,0]    # 4=1+1+2
 print   l   #next iteration will print :[0,2,0,2,0,0,0,0,0,0,0,0]    # 4=2+2
 print   l   #next iteration will print :[1,0,0,3,0,0,0,0,0,0,0,0]    # 4=1+3
 print   l   #last iteration will print :[0,0,0,4,0,0,0,0,0,0,0,0]    # 4=4
列表中的序列对我来说并不重要,但元素必须始终是=>9 应该是总和==n的所有可能的数字集。剩余部分用零填充

l=[0,0,0,0,0,0,0,0,0,0,0,0]
n = 11

for i in range (0,n):
        l[i] = 1 

print l
carryidx = 0 
for i in reversed(range(0,n - 1)):
        v = l[ n - 1] + l[i]
        if v > 9:
                while l[carryidx] > 9:
                        carryidx += 1
                l[carryidx] += 1
        else:
                l[n - 1] = v 
        l[i] = l[i] - 1 
        print l
输出

[1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0]
[1, 1, 1, 1, 0, 2, 0, 0, 0, 0, 0, 0]
[1, 1, 1, 0, 0, 3, 0, 0, 0, 0, 0, 0]
[1, 1, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0]

问题很不清楚,为什么在每个示例中都有多个列表?这些数字背后的逻辑是什么?如果n>len(l)呢?错误?我也不明白编号背后的逻辑。另外,你可能想要一个列表列表,而不是重复地重新分配
l
。如果列表一开始就用零填充,n将不大于len(l),你用什么方式填充它?几乎可以,sry我将编辑问题[0,2,0,2,0,2,0,0,0,0,0,0,0]为什么不在那里?@gnibbler,这回答了一个更老的问题,这个问题的解释不清楚