簡體   English   中英

如何打印與原始列表總和相同的列表的所有可能組合?

[英]How to print all possible combinations of a list that have the same sum as the original list?

我正在嘗試打印列表的所有可能組合,但前提是組合加起來是一個數字。

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:循環中的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)
>>>>>>>>>

令列表為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

如果我正確理解了您的代碼,您將繼續覆蓋這些值,而只剩下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)

實際上,您只需要對元素進行循環處理,因為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)]))
>>> 

另一個可能的解決方案:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM