[英]Create a list from a list of dictionaries subject to a condition (counter and accumulator) in python
我有一個物品清單字典:
ListDictItem = [ {'Item No': 1,'Weight':610,'Quantity':2},{'Item No': 2,'Weight':610,'Quantity':2},{'Item No': 3,'Weight':500,'Quantity':2},{'Item No': 4,'Weight':484,'Quantity':2},{'Item No': 5,'Weight':470,'Quantity':2},{'Item No': 6,'Weight':440,'Quantity':2},{'Item No': 7,'Weight':440,'Quantity':2},{'Item No': 8,'Weight':400,'Quantity':2}]
我從上面創建了一個權重列表,如下所示:
ItemWeigths: [610.0, 610.0, 500.0, 484.0, 470.0, 440.0,440, 400.0]
我想將這些物品包裝在架子上,以使每個架子的總重量小於特定值,並具有以下格式的列表:
shelves = [[{'Item No': 1,'Weight':610,'Quantity':2}],[{'Item No': 2,'Weight':610,'Quantity':2}],[{'Item No': 3,'Weight':500,'Quantity':2}],[{'Item No': 4,'Weight':484,'Quantity':2}], [{'Item No': 7,'Weight':440,'Quantity':2}],[{'Item No': 8,'Weight':400,'Quantity':2}] ]
其中特定架子的所有重量的總和<= 610
我創建了一個代碼,該代碼可以正確包裝第一個架子,但是我無法編寫用於創建其他物品的架子的代碼:
shelves=[]
ShelvesWeightTotal = 0
shelf=[]
new=[]
ShelfToPack=[]
for i in range(0,len(ItemWeigths)):
while ShelvesWeightTotal + ItemWeigths[i] <= WeightOfObject:
shelves += [ItemWeigths[i]]
ShelvesWeightTotal = sum(shelves)
上面的代碼適用於第一個項目,但是為了為其余項目創建其他貨架,我編寫了不起作用的代碼:
for i in range(0,len(ItemWeigths)):
while ItemsToCut !=[]:
if ShelvesWeightTotal + ItemWeigths[i] <=WeightOfObject:
shelves += [ItemWeigths[i]]
ShelvesWeightTotal = sum(shelves)
ItemWeigths.pop(i)
ItemsToCut.pop(i)
shelf.append(shelves)
任何建議編寫代碼以更好的方式創建架子的建議將不勝感激。
我采用了一種更簡單的方法:
ListDictItem = [{'Item No': 1,'Weight':610,'Quantity':2},
{'Item No': 2,'Weight':610,'Quantity':2},
{'Item No': 3,'Weight':500,'Quantity':2},
{'Item No': 4,'Weight':484,'Quantity':2},
{'Item No': 5,'Weight':470,'Quantity':2},
{'Item No': 6,'Weight':440,'Quantity':2},
{'Item No': 7,'Weight':440,'Quantity':2},
{'Item No': 8,'Weight':400,'Quantity':2}]
maxWeight = 610
shelves = []
shelf = []
current_shelf_weight = 0
for item in ListDictItem:
if current_shelf_weight + item['Weight'] <= maxWeight:
shelf.append(item)
current_shelf_weight += item['Weight']
else:
shelves.append(shelf)
if item['Weight'] <= maxWeight:
shelf = [item]
current_shelf_weight = item['Weight']
else:
shelf = []
current_shelf_weight = 0
if shelf: #append any remaining items
shelves.append(shelf)
print("shelves = " + str(shelves))
當我檢查此代碼的輸出時,得到以下信息:
shelves = [[{'Weight': 610, 'Item No': 1, 'Quantity': 2}], [{'Weight': 610, 'Item No': 2, 'Quantity': 2}], [{'Weight': 500, 'Item No': 3, 'Quantity': 2}], [{'Weight': 484, 'Item No': 4, 'Quantity': 2}], [{'Weight': 470, 'Item No': 5, 'Quantity': 2}], [{'Weight': 440, 'Item No': 6, 'Quantity': 2}], [{'Weight': 440, 'Item No': 7, 'Quantity': 2}], [{'Weight': 400, 'Item No': 8, 'Quantity': 2}]]
正是您想要的。
ListDictItem = [ {'Item No': 1,'Weight':610,'Quantity':2},{'Item No': 2,'Weight':610,'Quantity':2},{'Item No': 3,'Weight':500,'Quantity':2},{'Item No': 4,'Weight':484,'Quantity':2},{'Item No': 5,'Weight':470,'Quantity':2},{'Item No': 6,'Weight':440,'Quantity':2},{'Item No': 7,'Weight':440,'Quantity':2},{'Item No': 8,'Weight':400,'Quantity':2}];
sh = []; #weight, itemNo
for a in ListDictItem:
for b in range(0, a['Quantity']):
for j in range(0, len(sh)):
if sh[j]['weight'] + a['Weight'] <= 610:
sh[j]['weight'] += a['Weight']
sh[j]['items'].append(a['Item No'])
else:
sh.append({'weight' : a['Weight'], 'items' : [a['Item No']]})
print(sh)
一個非常簡單的解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.