![](/img/trans.png)
[英]How to find every combination of numbers that sum to a specific X given a large array of numbers?
[英]How to find a sum of combination given specific group?
我正在尋求幫助以找到所有可能的組合總和<一定數量。 給定下表:
[-inf,546) [546, 551) [551, 556)
0 $0.00 $0.00 $0.00
100 $29.37 $26.90 $25.81
200 $58.74 $53.80 $51.62
300 $88.12 $80.70 $77.43
400 $117.49 $107.61 $103.24
500 $146.86 $134.51 $129.04
組合示例:
等等
我不確定我可以搜索什么樣的關鍵字來搜索這類問題。 如果有人可以提供幫助,我真的很感激。
以下是如何使用來自itertools
的combinations
。
在此示例中,條件是總和必須小於20
:
from itertools import combinations
nums = [12, 21, 1, 3, 6, 7, 54, 2, 13, 32]
combs = []
for comb in combinations(nums, 3):
c = sum(comb)
if c not in combs and c < 20:
combs.append(c)
print(c)
print(combs)
Output:
16
19
15
17
10
11
6
14
9
12
18
[16, 19, 15, 17, 10, 11, 6, 14, 9, 12, 18]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.