【问题标题】:Sum all the possible combination of elements in a list [duplicate]总结列表中所有可能的元素组合[重复]
【发布时间】:2017-01-13 21:13:46
【问题描述】:

在 Python 中,我试图将元素的所有不同组合添加到 N 列表的列表中,其中 N 是一个变量。具体来说,我正在处理一个包含列表[1, 2, 3, 4, 5, 6] 的N 个副本的列表。

让我们假设N = 3。我想循环浏览第一个列表,添加:

 6 + 6 + 6
 5 + 6 + 6
 ...
 1 + 6 + 6`

然后将第二个人增加一次并开始添加:

6 + 5 + 6
5 + 5 + 6
...
1 + 1 + 1  # ultimately to

当然,我增加了第三个人。这个具体的例子可以用 3 个嵌套的 for 循环来完成,但当然,我需要改变循环的数量。我在网上和这个论坛上搜索答案,并不太能掌握我应该做什么。人们一直在说要使用递归函数,但直到我查到这个,我才听说过。所以有点难以掌握。

【问题讨论】:

  • 你尝试了什么,你能告诉我们你的尝试吗?也许我们可以帮助您修复它们。
  • SO 既不是代码编写也不是教程服务。评论How to Ask
  • 使用itertools.productlist(map(sum, product([1,2,3,4,5,6], repeat=n)))
  • 以下回复有所帮助。我还没有真正编写过尝试,因为我想不出/找到一个想法。我到处找,找不到任何我能理解的东西,包括 Tadhg McDonald-Jensen 发布的链接。如果有人觉得我问了一个不好的问题或滥用了该网站,我深表歉意,但我不知道在哪里可以找到我的问题的答案,我想我会自己问。感谢大家的帮助。

标签: python list


【解决方案1】:

为了实现这一点,您可以使用itertools.product 作为:

from itertools import product
my_list = [1, 2, 3, 4, 5, 6]
N=3 

#                         value of `N` v
for s in product(my_list[::-1], repeat=N):
    print '{} : {}'.format(s[::-1], sum(s))
    #           No need to reverse here ^
    #           since sum(s[::-1]) and sum(s) will give same result 

将打印:

# (combination) : sum
(6, 6, 6) : 18
(5, 6, 6) : 17
(4, 6, 6) : 16
(3, 6, 6) : 15
(2, 6, 6) : 14
(1, 6, 6) : 13
...
(6, 1, 1) : 8
(5, 1, 1) : 7
(4, 1, 1) : 6
(3, 1, 1) : 5
(2, 1, 1) : 4
(1, 1, 1) : 3

如果您只想将值存储在list 中,您可以使用 list comprehension 表达式为:

>>> [sum(s) for s in product(my_list[::-1], repeat=3)]
[18, 17, 16, 15, 14, 13, 17, 16, 15, 14, 13, 12, 16, 15, 14, 13, 12, 11, 15, 14, 13, 12, 11, 10, 14, 13, 12, 11, 10, 9, 13, 12, 11, 10, 9, 8, 17, 16, 15, 14, 13, 12, 16, 15, 14, 13, 12, 11, 15, 14, 13, 12, 11, 10, 14, 13, 12, 11, 10, 9, 13, 12, 11, 10, 9, 8, 12, 11, 10, 9, 8, 7, 16, 15, 14, 13, 12, 11, 15, 14, 13, 12, 11, 10, 14, 13, 12, 11, 10, 9, 13, 12, 11, 10, 9, 8, 12, 11, 10, 9, 8, 7, 11, 10, 9, 8, 7, 6, 15, 14, 13, 12, 11, 10, 14, 13, 12, 11, 10, 9, 13, 12, 11, 10, 9, 8, 12, 11, 10, 9, 8, 7, 11, 10, 9, 8, 7, 6, 10, 9, 8, 7, 6, 5, 14, 13, 12, 11, 10, 9, 13, 12, 11, 10, 9, 8, 12, 11, 10, 9, 8, 7, 11, 10, 9, 8, 7, 6, 10, 9, 8, 7, 6, 5, 9, 8, 7, 6, 5, 4, 13, 12, 11, 10, 9, 8, 12, 11, 10, 9, 8, 7, 11, 10, 9, 8, 7, 6, 10, 9, 8, 7, 6, 5, 9, 8, 7, 6, 5, 4, 8, 7, 6, 5, 4, 3]

【讨论】:

  • 谢谢,这正是我所需要的。我非常感谢您的回复。
【解决方案2】:

您可以使用itertools.combinations_with_replacement来解决这个问题。

In []: from itertools import combinations_with_replacement
In []: for combination in combinations_with_replacement(range(1, 7), 3):
           print("Combination: ", combination, " Sum: ", sum(combination))
   ....:     
Combination: (1, 1, 1)  Sum: 3
Combination: (1, 1, 2)  Sum: 4
Combination: (1, 1, 3)  Sum: 5
Combination: (1, 1, 4)  Sum: 6
Combination: (1, 1, 5)  Sum: 7
Combination: (1, 1, 6)  Sum: 8
.
.
.
Combination: (4, 5, 6)  Sum: 15
Combination: (4, 6, 6)  Sum: 16
Combination: (5, 5, 5)  Sum: 15
Combination: (5, 5, 6)  Sum: 16
Combination: (5, 6, 6)  Sum: 17
Combination: (6, 6, 6)  Sum: 18

如果您想将总和存储在 list 中,您可以使用 列表推导

list_of_sums = [sum(combination) for combination in combinations_with_replacement(range(1, 7), 3)]

【讨论】:

  • 这对我也有用。谢谢。
猜你喜欢
  • 2018-05-15
  • 2023-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多