【问题标题】:How to print all possible combinations of a list that have the same sum as the original list?如何打印与原始列表具有相同总和的列表的所有可能组合?
【发布时间】:2017-08-17 14:13:08
【问题描述】:

我正在尝试打印列表的所有可能组合,但前提是这些组合加起来是一个数字。

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)
>>>>>>>>>

【问题讨论】:

  • 您的要求似乎搞砸了。你说你想要总和到列表总数的组合......但是你传递了一个总数。它是哪个?问题部分在于for...您为列表中的每个项目执行整个算法。尝试使用 sum(lst) 并完全跳过那些添加 for 循环。你的代码会更清晰。
  • 是的,我也有同样的疑问,所以我在答案中添加了一个sum_list 变量(根据您的实际要求进行更改)。你也只需要那些长度= 2的列表吗?请进行适当的更改。
  • 总的来说,最好把它作为一个论据,因为它可能会不时改变。列表的长度可以超过 2,但这是一个非常简化的版本,它帮助我思考并实际解决了真正的问题。非常感谢
  • 我已编辑问题以反映这一点

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


【解决方案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

【讨论】:

    【解决方案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
    

    【讨论】:

    • 加上那些不是所有的组合,而只是长度为 2 的组合,对吧?因此,如果您制作更大的列表,您的代码将无法扩展。希望能满足你的要求。
    【解决方案3】:

    您实际上只需要对元素进行循环,因为您的 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)]))
    >>> 
    

    【讨论】:

      【解决方案4】:

      如果我正确理解您的代码,您只需继续覆盖这些值,只留下 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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-28
        • 2019-02-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-09
        相关资源
        最近更新 更多