[英]Finding all possible permutations of a fixed length of numbers to reach a given sum
[英]Given a list of numbers find all the permutations that add to 100 of a certain length
给定一个数字list = [0,1,2,10,21,25,30,33,34,35]
,我想找到在 python 上总和为 100 的这些数字的所有排列。
但是我希望能够多次使用列表中的值,并且我希望能够 select 排列的长度。
例如,如果排列的长度是 3,我会返回: [[30, 35, 35], [33, 33, 34], [33, 34, 33], [34, 33, 33], [35, 30, 35], [35, 35, 30]]
。
我已经能够为长度 3 执行此操作。但是,我找到了总和为 100 的 3 个数字的所有组合,然后对其进行过滤以删除包含原始列表之外数字的解决方案。
问题是我不能按照这种方法来处理长度为 4、5、6 的排列,因为有更多的组合。
谢谢
我不确定我是否正确理解了您的问题,但试试这个:
from itertools import product
def special_permutation(lst, rno, suma):
results = []
for c in product(lst, repeat = rno):
if sum(c) == suma:
results.append(c)
return results
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.