場所と数量は辞書で管理されているため、場所で利用可能な数量を見つけるロジックを作成しました。
d={'loc2': 500.0, 'loc3': 200.0, 'loc1': 1000.0, 'loc4': 100.0, 'loc5': 50.0}
from operator import itemgetter
def find_combination(locs,qty):
locs = sorted(locs.items(),key=itemgetter(1),reverse=True)
result = []
for loc,val in locs:
if qty <= 0:
break
elif qty - val >= 0:
qty -= val
result.append((loc,val))
return result
数量が口述の最大数量を下回ると、これらの予期しない結果が得られます。
print find_combination(d,1000)
[('loc1', 1000.0)]
print find_combination(d,750)
[('loc2', 500.0), ('loc3', 200.0), ('loc5', 50.0)]
print find_combination(d,1900)
[('loc1', 1000.0), ('loc2', 500.0), ('loc3', 200.0), ('loc4', 100.0), ('loc5', 50.0)]
print find_combination(d,150)
[('loc4', 100.0), ('loc5', 50.0)]
print find_combination(d,34)
[] # unexpected # should be [('loc5', 50.0)]
print find_combination(d,88)
[('loc5', 50.0)] # should be [('loc4', 100.0)]