给定一个数字列表,找到所有加到特定长度的 100 的排列

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,我会 return: [[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