我如何将列表划分为总和为给定数字(不重复)的子集?

how can i divide a list as subsets which are sum upto given number(non repeat)?

来自给定的号码列表

nums=[4,3,2,3,5,2,1]

from itertools import combinations
nums=[4,3,2,3,5,2,1]
li=[]
for i in range(1,len(nums)):
    comb=combinations(nums,i)
    for j in comb:
        if sum(j)==5:
            li.append(j)

print(li)

输出为

[(5,), (4, 1), (3, 2), (3, 2), (2, 3), (3, 2), (2, 2, 1)]

我能找到子集,但元素似乎重复了 对非重复元素很感兴趣

我想要总和等于 5 的子集列表 (不重复)

example: [(5), (1, 4), (2,3), (2,3)]

如果您稍微更改循环以便从列表中删除使用过的数字,则它们不会在另一个总和中重复使用,例如。 g.

i = 1
while i <= len(nums):
    comb = combinations(nums, i)
    for j in comb:
        if sum(j) == 5:
            li.append(j)
            for n in j: nums.remove(n)
            break
    else: i += 1    # increment only if nothing found