我有一个包含位置和数量的字典,比如
{'loc1': 1000.0,'loc2': 500.0, 'loc3': 200.0,'loc4': 100.0,'loc5': 50.0, }
现在,当我订购时,情景应如下所示,
150 quantity
,它应该从loc5
和loc4
210 quantity
,它应该从loc3
和loc5
1777 quantity
,它应该从loc1
和loc2
以及loc3
和loc4
530 quantity
,它应该从loc2
和loc5
获取产品。我不知道如何实现这样的条件,任何人都可以解决它吗?
答案 0 :(得分:7)
将数量放入列表中,排序。 Use bisect
to find an appropriate quantity.计算较低的数量是否可以满足,如果不是,则选择下一个更高的数量。减去选定的数量。如果仍然大于0,请返回bisect
步骤。
修改强>
import bisect
qtys = [50, 100, 200, 500, 1000]
def sack(amt, qtys=qtys):
res = set()
while amt > 0:
pivot = bisect.bisect(qtys, amt)
if sum(qtys[:pivot]) >= amt:
amt -= qtys[pivot - 1]
res.add(pivot - 1)
else:
if sum(qtys[:pivot + 1]) < amt:
raise ValueError('Not enough items to fill the sack')
res.add(pivot)
amt -= qtys[pivot]
return res
print sack(150)
print sack(210)
print sack(1777)
print sack(530)
答案 1 :(得分:1)
def find_combination(d,val):
"""(dict,int)->list
Given a dict with values as numbers, returns the combination of keys whose values sums up to "val"
In case no values form a perfect sum, picks up the next best case
"""
new_list = sorted(d.items(),key=lambda y: y[1],reverse=True)
result = []
while val > 0:
min_item = ''
for item in new_list:
if item[0] in result:
continue
new_diff = abs(val - 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)
val = val - min_val
return result
鉴于
d={'loc2': 500.0, 'loc3': 200.0, 'loc1': 1000.0, 'loc4': 100.0, 'loc5': 50.0}
这给出了
>>> combi.find_combination(d,150)
['loc4', 'loc5']
>>> combi.find_combination(d,210)
['loc3', 'loc5']
>>> combi.find_combination(d,1777)
['loc1', 'loc2', 'loc3', 'loc4']
>>> combi.find_combination(d,530)
['loc2', 'loc5']
>>> combi.find_combination(d,160)
['loc3']
必须指出它(可怕地)效率低下
答案 2 :(得分:0)
此算法可能对您有帮助!
def greedy(amount, denoms):
result = []
while (amount > 0):
print amount, denoms, result
if (amount >= denoms[0]):
num = amount // denoms[0]
amount -= (num * denoms[0])
result.append([denoms[0], num])
denoms = denoms[1:]
return result
print greedy(100, [25,10,5,1])
print ""
print greedy(100, [10,5,1])
print ""
print greedy(100, [5,1])
print ""
print greedy(100, [1])
print ""
print greedy(47, [25,10,5,1])
输出将是
100 [25, 10, 5, 1] []
[[25, 4]]
100 [10, 5, 1] []
[[10, 10]]
100 [5, 1] []
[[5, 20]]
100 [1] []
[[1, 100]]
47 [25, 10, 5, 1] []
22 [10, 5, 1] [[25, 1]]
2 [5, 1] [[25, 1], [10, 2]]
2 [1] [[25, 1], [10, 2]]
[[25, 1], [10, 2], [1, 2]]
答案 3 :(得分:0)
即使我认为Ignacio和Atul的答案更好,如果你想坚持使用字典,你可以使用一个减去数量的while循环,
#qnt,the quantity you have
dic={'loc1': 1000.0,'loc2': 500.0, 'loc3': 200.0,'loc4': 100.0,'loc5': 50.0, }
dic2= {'loc1':0,'loc2':0,'loc3':0,'loc4':0,'loc5':0}
while qnt>0:
if qnt>=dic['loc1']:
qnt-= dic['loc1']
dic2['loc1']+=1
elif qnt>=dic['loc2']:
qnt-= dic['loc2']
dic2['loc2']+=1
elif qnt>=dic['loc3']:
qnt-= dic['loc3']
dic2['loc3']+=1
elif qnt>=dic['loc4']:
qnt-= dic['loc4']
dic2['loc4']+=1
elif qnt>0:
qnt-= dic['loc5']
dic2['loc5']+=1
else:break
print dic2