簡體   English   中英

如何找到給定特定組的組合總和?

[英]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

組合示例:

  1. $0.00 + $0.00 + $0.00 {0: [-inf,546), 0: [546, 551), 0:[551, 556)}
  2. 0.00 美元 + 0.00 美元 + 25.81 美元 {0: [-inf,546), 0: [546, 551), 100:[551, 556)}
  3. 0.00 美元 + 0.00 美元 + 51.62 美元 {0: [-inf,546), 0: [546, 551), 200:[551, 556)}
  4. 29.37 美元 + 0.00 美元 + 0.00 美元 {100: [-inf,546), 0: [546, 551), 200:[551, 556)}

等等

我不確定我可以搜索什么樣的關鍵字來搜索這類問題。 如果有人可以提供幫助,我真的很感激。

以下是如何使用來自itertoolscombinations

在此示例中,條件是總和必須小於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.

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