我已经写了一个逻辑来查找位置中的可用数量,因为位置和数量是通过字典进行管理的,
d={'loc2': 500.0, 'loc3': 200.0, 'loc1': 1000.0, 'loc4': 100.0, 'loc5': 50.0}
def find_combination(locations,qty):
new_list = sorted(locations.items(),key=lambda y: y[1],reverse=True)
result = []
while qty > 0:
min_item = ''
for item in new_list:
if item[0] in result:
continue
new_diff = abs(qty - item[1])
if not min_item or new_diff <= min_diff:
min_item = item[0]
min_diff = new_diff
min_val = item[1]
result.append((min_item ,locations.get(min_item)))
qty = qty - min_val
return result
现在,当数量小于字典中的最大数量时,它会给出意外的结果,
print find_combination(d,500)
OUTPUT: [('loc2', 500.0)]
print find_combination(d,1000)
OUTPUT: [('loc1', 1000.0)]
print find_combination(d,750)
OUTPUT: [('loc2', 500.0), ('loc3', 200.0), ('loc5', 50.0)]
print find_combination(d,1800)
OUTPUT: [('loc1', 1000.0), ('loc1', 1000.0)] # unexpected
qq_笑_17
慕娘9325324
相关分类