如何打印与原始列表具有相同总和的列表的所有可能组合?

时间:2017-03-24 15:05:13

标签: python list for-loop while-loop itertools

我正在尝试打印列表的所有可能组合,但仅限于组合加起来的数字。

lst = [0, 1, 2] #The goal is print all combinations that sum up to 3

import itertools

def fun(lst, total):
    sum = 0
    for element in lst:
        sum += element
        all_possible_combinations = set(itertools.product(lst, repeat=2)) # This prints all possible combinations of the elements in the list with length of 2
        for items in all_possible_combinations:
            a = 0
            for i in items:
                a += i
            if a == total:
                x = items
                print(x)
    print('These are all combinations:', all_possible_combinations)

fun([0, 1, 2], 2)

此程序打印总和为3的列表,但不止一次打印这些列表。

>>>>>>>>
(2, 0)
(1, 1)
(0, 2)  #I was expecting the programme to stop here.
(2, 0)
(1, 1)
(0, 2)
(2, 0)
(1, 1)
(0, 2)
These are all combinations: {(0, 1), (1, 2), (0, 0), (2, 1), (2, 0), (1, 1), (2, 2), (1, 0), (0, 2)}
>>>>>>>>

我认为这是因为for element in lst:循环所以我试图在这个循环之外打印

import itertools

def fun(lst, total):
    sum = 0
    for element in lst:
        sum += element
        all_possible_combinations = set(itertools.product(lst, repeat=2)) # This prints all possible combinations of the elements in the list with length of 2
        for items in all_possible_combinations:
            a = 0
            for i in items:
                a += i
            if a == total:
                x = items
    print(x)
    print('These are all combinations:', all_possible_combinations)

fun([0, 1, 2], 2)

此程序仅返回其中一个列表

>>>>>>>>>
(0, 2)
>>>>>>>>>

我该如何纠正?我期待最终的结果是

>>>>>>>>>
(2, 0)
(1, 1)
(0, 2)
>>>>>>>>>

4 个答案:

答案 0 :(得分:2)

让列表为l = [0, 1, 2]sum_list = sum(l)编辑:抱歉初读错误

然后你可以这样做:

import itertools as it

for i in range(len(l)):
    ans = list(filter(lambda x: sum(x)==sum_list, list(it.combinations(l, i)))

print ans

答案 1 :(得分:1)

如果我正确理解你的代码,你只需要覆盖这些值,只留下x的最后一个值。创建数组并将项目附加到此数组应显示所有结果。

import itertools

def fun(lst, total):
    sum = 0
    for element in lst:
        sum += element
        x = []
        all_possible_combinations = set(itertools.product(lst, repeat=2)) # This prints all possible combinations of the element in the list with length of 2
        for items in all_possible_combinations:
            a = 0
            for i in items:
                a += i
            if a == total:
                x.append(items)
    print(x)
    print('These are all combinations:', all_possible_combinations)

fun([0, 1, 2], 2)

答案 2 :(得分:1)

你实际上只需要对元素进行循环,因为你的all_possible_combinations确实在列表中具有2元组的所有组合。循环遍历lst时,它会重复进程列表的长度,从而重复输出。

修订第一版:

import itertools

def fun(lst, total):
        all_possible_combinations = set(itertools.product(lst, repeat=2)) # This prints all possible combinations of the elements in the list with length of 2
        for items in all_possible_combinations:
            a = 0
            for i in items:
                a += i
            if a == total:
                x = items
                print(x)
        print('These are all combinations:', all_possible_combinations)

fun([0, 1, 2], 2)

输出:

>>> fun([0, 1, 2], 2)
(1, 1)
(2, 0)
(0, 2)
('These are all combinations:', set([(0, 1), (1, 2), (0, 0), (2, 1), (1, 1), (2, 0), (2, 2), (1, 0), (0, 2)]))
>>> 

答案 3 :(得分:1)

另一种可能的解决方案:

import itertools

a = [0, 1, 2]

result = [b for b in itertools.product(a, repeat=2) if sum(b)<=sum(a)]

为了获得你想要的东西,这就是代码:

import itertools

a = [0, 1, 2]

result = [b for b in itertools.product(a, repeat=2) if sum(b)<sum(a) and sum(b)>1]
print result