可以使用combinations
從itertools
模塊和max()
,像這樣的例子解決你的問題:
from itertools import combinations
orders = [39.789032, 36.023618, 3.0539913, 20.0, 1.314, 2.5, 0.401, 1.0, 1.989, 6.3254668, 0.5]
startingvolume = 100
sub = []
for k in range(1, len(orders)):
sub.append(max([[j, sum(j)] for j in combinations(orders, k) if sum(j) <= startingvolume], key = lambda x: x[1]))
best_order, fill_max = max(sub, key = lambda x: x[1])
print("best order is: {}\nMax to fill from starting volume is: {}"
.format(best_order, fill_max))
輸出:
best order is: (39.789032, 36.023618, 3.0539913, 20.0, 1.0)
Max to fill from starting volume is: 99.86664
等都不是免費的編碼服務。你必須嘗試自己解決問題。如果無法正常工作,請發佈您嘗試的內容,我們會幫助您解決問題。 – Barmar
這是功課嗎? – jordanm
這看起來像揹包問題https://en.wikipedia.org/wiki/Knapsack_problem。這是一個非常困難的問題,許多偉大的思想家都在努力解決問題。 – setholopolus