在python中根据条件(计数器和累加器)的字典列表创建一个列表

时间:2016-08-27 18:54:50

标签: python list python-3.x dictionary

我有一个项目列表字典:

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)

任何建议以更好的方式编写代码来创建货架的建议都将受到赞赏。

2 个答案:

答案 0 :(得分:4)

我采取了一种更简单的方法:

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}]]

这正是你想要的。

答案 1 :(得分:0)

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)

解决问题的一个非常简单的方法。