简体   繁体   English

对具有约束的列表元素的组合求和

[英]Summing combinations for the elements of a list with a constraint

I have a nested list g=[[2, 1],[1, 3],[8, 1]] .我有一个嵌套列表g=[[2, 1],[1, 3],[8, 1]] I want to have a list in which:我想要一个列表,其中:

every inner element of the list added up to at most another two inner elements.列表的每个内部元素最多加起来另外两个内部元素。 Here's the process and therefore desired output:这是过程,因此需要 output:

# 1= a single element like g[0][1]
# 2= a single element like g[0][0] or can be added by two inner elements like g[0][1]+g[1][0]
# 3= a single g[1][1], adding two elements g[0][0]+g[1][0] or at most three g[0][1]+g[1][0]+g[2][1] 
.
.
.
# 13= g[0][0]+g[1][1]+g[2][0]

So the final result will be a list.所以最终的结果将是一个列表。 Notice there can't be 7 in the results since there's no combination (without replacement) that can be add up to 7. Also from each element at most one inner value can be selected.请注意,结果中不能有 7,因为没有可以加起来为 7 的组合(无需替换)。此外,每个元素最多可以选择一个内部值。

expected_result = [1,2,3,4,5,6,8,9,10,11,12,13]

This is what I've done but it doesn't contain 1 and 2 and it also contains 7:这就是我所做的,但它不包含 1 和 2,它还包含 7:

g=[[2, 1],[1, 3],[8, 1]]
from itertools import product
maxx = []
# Getting all possible combination of adding at most 3 elements (=length of g)
for i in list((product([i for j in g for i in j], repeat=len(g)))):
    maxx.append(sum(i))
    # Narrowing the result so it doesn't exceed the maximum combination which is sum([2,3,8])
print([i for i in set(maxx) if i<=sum(max_list)])

>>> [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]

Any help is appreciated.任何帮助表示赞赏。

It seems like the shape/format of the data is a red herring.数据的形状/格式似乎是一条红鲱鱼。 Just flatten the list and looks for the unique sums of each combination of 1, 2, or 3 elements of the flattened list.只需展平列表并查找展平列表中 1、2 或 3 个元素的每个组合的唯一总和。 Use itertools.chain to flatten the list, itertools.combinations to create the combinations, itertools.chain again to combine the combinations, and a set to return unique results:使用itertools.chain展平列表,使用itertools.combinations创建组合,再次使用itertools.chain组合组合,以及返回唯一结果的set

>>> import itertools
>>> g = [[2, 1],[1, 3],[8, 1]]
>>> flattened = itertools.chain(*g)
>>> flattened
[2, 1, 1, 3, 8, 1]
>>> list(set(map(sum,
  itertools.chain(itertools.combinations(flattened, 1), 
                  itertools.combinations(flattened, 2),
                  itertools.combinations(flattened, 3)))))
[1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13]

Edit : The question was changed to include the following constraint:编辑:问题已更改为包括以下约束:

Also from each element at most one inner value can be selected.也可以从每个元素中选择最多一个内部值。

That means you can't flatten the list.这意味着您不能展平列表。 Here's an updated solution that satisfies the new constraint:这是满足新约束的更新解决方案:

>>> list(set(map(sum,
        itertools.chain.from_iterable(itertools.combinations(p, n)
        for n in range(1,4)
        for p in itertools.product(*g)))))
[1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13]

>>> g = [[2, 1],[1, 3],[8, 1],[14, 15]  # should not produce 29 as an answer
>>> list(set(map(sum,
        itertools.chain.from_iterable(itertools.combinations(p, n)
        for n in range(1,4)
        for p in itertools.product(*g)))))
[1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 22, 23, 24, 25, 26]

The simpliest implementation that I can think of now:我现在能想到的最简单的实现:

def fun(numbers):
    all_numbers = [x for y in numbers for x in y]
    output = list(set(all_numbers))
    for x in all_numbers:
        for y in all_numbers:
            if x is not y:
                output.append(x + y)
            for z in all_numbers:
                if y is not x and y is not z and x is not z:
                    output.append(x + y + z)
    return list(set(output))


print(fun([[2, 1], [1, 3], [8, 1]]))

Above code prints上面的代码打印

[1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM