如何清除子集总和时解决此错误?

时间:2018-10-01 07:05:18

标签: python math

我正在做一个项目,要求我找到一定值的总和分区(required_reduction)。我必须清除子列表中的total_parts列表(如果它具有除梳子的子列表的索引2以外的其他任何值)。

Example:
required_reduction = 4 
combs = [[2,1],[3,2]]
total_parts = [[1,1,1,1],[1,1,2],[2,2],[1,3]]

我不能使用[1,3],因为其中有“ 3”而不是包含在1和2的“ combs”子列表的2.elemnt中的值。“ partition”函数取自stackoverflow ,我正在尝试建立休息。

import numpy as np


required_reduction = 16


combs = [[2,4],[3,5],[4,8],[5,13],[8,15],[10,18]]


def partition(number):
    answer = set()
    answer.add((number, ))
    for x in range(1, number):
        for y in partition(number - x):
            answer.add(tuple(sorted((x, ) + y)))
    return answer

total_parts = partition(required_reduction)
total_parts = list(total_parts)



def applicable_partitions(total_partitions, combinations):
    selections = [0]
    for _ in range(0, len(combinations) - 1):
        selections.append(combinations[_][1])
    del selections[0]
    possible_partitions = np.array(np.zeros(1,int))


    for i in range(0, len(total_partitions)-1):
        for j in range(0, len(total_partitions[i]) -1):
            for k in range(0, len(selections)-1):
                if total_partitions[i][j] == selections[k]:
                    continue
                else:
                    total_partitions.remove(total_partitions[i])

    return total_partitions

print(applicable_partitions(total_parts, combs))

我收到此错误:

  

如果total_partitions [i] [j] ==选择[k]:   IndexError:元组索引超出范围。

有人可以看到原因吗?

0 个答案:

没有答案