[英]Summing combinations for the elements of a list with a constraint
我有一个嵌套列表g=[[2, 1],[1, 3],[8, 1]]
。 我想要一个列表,其中:
列表的每个内部元素最多加起来另外两个内部元素。 这是过程,因此需要 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]
所以最终的结果将是一个列表。 请注意,结果中不能有 7,因为没有可以加起来为 7 的组合(无需替换)。此外,每个元素最多可以选择一个内部值。
expected_result = [1,2,3,4,5,6,8,9,10,11,12,13]
这就是我所做的,但它不包含 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]
任何帮助表示赞赏。
数据的形状/格式似乎是一条红鲱鱼。 只需展平列表并查找展平列表中 1、2 或 3 个元素的每个组合的唯一总和。 使用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]
编辑:问题已更改为包括以下约束:
也可以从每个元素中选择最多一个内部值。
这意味着您不能展平列表。 这是满足新约束的更新解决方案:
>>> 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]
我现在能想到的最简单的实现:
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]]))
上面的代码打印
[1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.