例如:
array = [4,3,2,0,0,0,0,0,0]第0指数只能与第3指数和第6指数相结合。第一指数只能与第四指数和第七指数相结合。第二指数只能与第五指数和第八指数相结合。(和应在这些索引之间保持不变)。那么,产出应是:
[1,2,2,1,1,0,2,0,0]
[2,1,1,1,1,1,1,1,0]...在这两个组合中,各个索引之间的和(上面列出)保持不变。
发布于 2022-05-14 10:17:34
使用由findPairs生成的answer to your previous question函数
from itertools import product
def findPairs(sum_value, len_value):
lst = range(sum_value + 1)
return [
pair
for pair in product(lst, repeat=len_value)
if sum(pair) == sum_value
]import itertools
combinations = itertools.product(findPairs(array[0], 3), findPairs(array[1], 3), findPairs(array[2], 3))
result = [list(itertools.chain(*zip(p1, p2, p3))) for p1, p2, p3 in combinations]
print(result[0:10])[[0, 0, 0, 0, 0, 0, 4, 3, 2], [0, 0, 0, 0, 0, 1, 4, 3, 1],
[0, 0, 0, 0, 0, 2, 4, 3, 0], [0, 0, 1, 0, 0, 0, 4, 3, 1],
[0, 0, 1, 0, 0, 1, 4, 3, 0], [0, 0, 2, 0, 0, 0, 4, 3, 0],
[0, 0, 0, 0, 1, 0, 4, 2, 2], [0, 0, 0, 0, 1, 1, 4, 2, 1],
[0, 0, 0, 0, 1, 2, 4, 2, 0], [0, 0, 1, 0, 1, 0, 4, 2, 1]]
...https://stackoverflow.com/questions/72238965
复制相似问题