Error in while loop code for packing items

GP gyan.am010 at gmail.com
Thu Aug 18 07:52:32 EDT 2016




I have a list dictionary of items:
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}]

I would like to pack the items in shelves such that the total weight of each shelf is less than a particular value. 

I have created a list of weights from above like this:
ItemWeigths: [610.0, 610.0, 500.0, 484.0, 470.0, 440.0,440, 400.0]


and a code which creates the first shelf :

shelves=[]
ShelvesWeightTotal = sum(shelves)
shelf=[]
new=[]
ShelfToPack=[]


for i in range(0,len(ItemWeigths)):
    while ShelvesWeightTotal <=WeightOfObject:
          shelf += [ItemWeigths[i]]
          ShelvesWeightTotal = sum(shelf)

for k in range(0,len(shelf)):
      q1=ListDictItem[k]
      q2 = ListDictItem.pop(k) #deletes the items that are packed.
      shelves.append(q1)
      new.append(q2)                       

ShelfToPack.append(shelves)

This code works but when I try to create other shelves, I made a while statement over the items that are remaining ,(while ItemsRemaining !=0) and I get an error for the code. The details are below:

Code:

while ListDictItem != []:

   for i in range(0,len(ItemsToCutLength)):
       while ShelvesLengthTotal <=LengthOfObject:
          shelves += [ItemWeigths[i]]
          ShelvesLengthTotal = sum(shelves)

   for k in range(0,len(shelves)):
      q1=ItemsToCut[k]
      q2 = ListDictItemt.pop(k) #deletes the items that are packed.
      shelf.append(q1)
      new.append(q2)                       
   ListDictItem==ListDictItem 
ShelfToPack.append(shelf)


Error message:Traceback (most recent call last):
  File "C:\Project\Python\ReadExcel-xlrd.py", line 201, in <module>
    q1=ListDictItem[k]
IndexError: list index out of range.


I would like the ShelfToPack list look like :

[[{'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}],[{..},...{..}],[{..},...{..}]]

Any suggestion in pointing the error or to improve the code will be appreciated.

Thanks in advance!! 

Cheers!
GP



More information about the Python-list mailing list