Error in while loop code for packing items
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
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
--
https://mail.python.org/mailman/listinfo/python-list
Re: Error in while loop code for packing items
On Thursday, August 18, 2016 at 5:59:43 PM UTC+5:30, Peter Otten wrote:
> GP wrote:
>
> The error and your second snippet aren't compatible, so I assume the
> exception is raised by
>
> > for k in range(0,len(shelf)):
> > q1=ListDictItem[k]
> > q2 = ListDictItem.pop(k) #deletes the items that are packed.
>
> > Error message:Traceback (most recent call last):
> > File "C:\Project\Python\ReadExcel-xlrd.py", line 201, in
> > q1=ListDictItem[k]
> > IndexError: list index out of range.
>
> Take a moment to understand what happens if you iterate over the loop index
> like above:
>
> items = ["foo", "bar", "baz"]
>
> for k in range(len(items)):
> item = items[k]
> print(item)
> items.pop(k)
>
> You start with k=0, item is set to items[0], i. e. "foo", that is printed
> and then items.pop(0) removes "foo" from the list which now looks like
>
> items = ["bar", "baz"]
>
> Now k=1, item is set to items[1], ie. "baz" ("bar" is never processed),
> "baz" is printed and then items.pop(1) removes "baz" and the list becomes
>
> items = ["bar"]
>
> Now k=2, so when you access items[2] from a list with only one item you get
> the IndexError. To make similar code work you need a while loop:
>
> while items:
> item = items.pop(0)
> print(item)
>
> However, when you really want to remove all items you instead assign a new
> empty list
>
> for item in items:
> print(item)
> items = []
Thanks Peter for the information. It helps to find the source of error .
What I wanted was to remove the items that was packed so that the code can
check for the next items that match the criteria and then pack those and delete
them and keep on repeating until all the items are packed. This is where I am
getting stuck.
--
https://mail.python.org/mailman/listinfo/python-list
Error numpy install
I have installed numpy using the command pip install numpy from command prompt and I am getting the following error: Traceback (most recent call last): File "", line 1, in import numpy File "C:\Users\GP\AppData\Local\Programs\Python\Python35\lib\site-packages\numpy\__init__.py", line 180, in from . import add_newdocs File "C:\Users\GP\AppData\Local\Programs\Python\Python35\lib\site-packages\numpy\add_newdocs.py", line 13, in from numpy.lib import add_newdoc File "C:\Users\GP\AppData\Local\Programs\Python\Python35\lib\site-packages\numpy\lib\__init__.py", line 8, in from .type_check import * File "C:\Users\GP\AppData\Local\Programs\Python\Python35\lib\site-packages\numpy\lib\type_check.py", line 11, in import numpy.core.numeric as _nx File "C:\Users\GP\AppData\Local\Programs\Python\Python35\lib\site-packages\numpy\core\__init__.py", line 14, in from . import multiarray ImportError: cannot import name 'multiarray' I have also tried to install multiarray but it says: "Could not find a program that satisfies the requirement multiarray(from versions) Any suggestions on how to install -- https://mail.python.org/mailman/listinfo/python-list
Suggestions to improve a code
I have a list:
shelves2 =[{'Part No': '1', 'Length': 610.0, 'Width': 50.0},
{'Part No': '2', 'Length': 2319.0, 'Width': 465.0 },
{'Part No': '3', 'Length': 5.0,'Width': 465.0}]
The length of shelf is calculated as follows:
1. Calculate the maximum length of all items in the shelf.
2. Calculate width of shelf = 610 (in this case).
3. Length of shelf : maxlength of all items + length ( if it less than width
of shelf) or width of other items such that length of shelf is less than
maximum allowed length.
In the above case the length is 2319+50+5 = 2374. I have the following code
which generates it correctly but would like to know if there is a better way to
write it. I would like to know some smarter way to write the code ( especially
if the "for", "if" statements can be avoided).
list_1=[]
list_2=[]
WidthOfShelf = max([x['Width'] for x in shelves2])
MaxLengthOfItem = max([x['Length'] for x in shelves2])
f_LMax=lambda seq, m: [ii for ii in range(0, len(seq)) if seq[ii] ['Length']
>= m][0]
MaxLengthOfItem_Index =f_LMax(shelves2,MaxLengthOfItem)
current= MaxLengthOfItem
list_1=[shelves2[MaxLengthOfItem_Index]]
list_2 = [MaxLengthOfItem]
del shelves2[MaxLengthOfItem_Index]
for i in range(0,len(shelves2)):
if shelves2[i]['Length'] <= Width:
if (current + shelves2[i]['Length']) or (current + shelves2
[i]['Width'])<= 2438.5 :
q_2= min(shelves2[i]['Length'], shelves2[i]['Width'])
q=shelves2[i]
list_1.append(q)
list_2.append(q_2)
current += (shelves2[i]['Length'] or shelves2[i] ['Width'])
length = sum(list_2)
print("LENGTH:",length)
--
https://mail.python.org/mailman/listinfo/python-list
Need some suggestions in grouping of items
I have a list of items:
ShelvesToPack = [{'ShelfLength': 2278.0, 'ShelfWidth': 356.0, 'ShelfArea':
759152.0, 'ItemNames': 1},
{'ShelfLength': 1220.0, 'ShelfWidth': 610.0, 'ShelfArea': 372100.0,
'ItemNames': 2},
{'ShelfLength': 2310.0, 'ShelfWidth': 762.0, 'ShelfArea': 1760220.0,
'ItemNames': 3},
{'ShelfLength': 610.0, 'ShelfWidth': 610.0, 'ShelfArea': 1450435.0,
'ItemNames': 4}]
I have a certain criteria and I want to group the items based on those.
I need the program that tells how many minimum number of groups one can have
and how the items are grouped. I would like to form groups of these items such
that (sum of shelflength (of itmes) <= max length or sum of shelfwidth (of
items) <= max width) and sum of ShelfArea (of items) <= Max Area. In this case,
if we look at the logic we can have all the items packed in minimum 2 groups -
item 1 and 3 will form one group and item 2 & 4 will form other group. I would
like to have the answer in the format:
[[{'ShelfLength': 2278.0, 'ShelfWidth': 356.0, 'ShelfArea':759152.0,
'ItemNames': 1} ,
{'ShelfLength': 2310.0, 'ShelfWidth':762.0, 'ShelfArea': 1760220.0,
'ItemNames': 3}],
[{'ShelfLength': 1220.0, 'ShelfWidth': 610.0, 'ShelfArea': 372100.0,
'ItemNames': 2},
, {'ShelfLength': 610.0, 'ShelfWidth': 610.0, 'ShelfArea': 1450435.0,
'ItemNames': 4}]]
I have written a code but it does only one grouping ( need some help why it
doesnot for the remaining items and what corrections need to be done) .
ShelvesToPack_sorted=sorted(ShelvesToPack, key = itemgetter('ShelfWidth'),
reverse = True)
AreaOfObject= 2972897.28
current_width = 0
current_length = 0
current_area =0
shelf =[]
shelves=[]
if len(ShelvesToPack_sorted) > 0:
for item in ShelvesToPack_sorted:
if (current_width + item['ShelfWidth'] <= 1219.2 or
current_length + item['ShelfLength'] <= 2438.5) and current_area +
item['ShelfArea'] <= AreaOfObject:
shelf.append(item)
current_width += item['ShelfWidth']
current_length += item['ShelfLength']
current_area += item['ShelfArea']
del item
if shelf:
shelves.append(shelf)
print(shelves)
This produces only one grouping and does not group the remaining items.
[[{'ShelfLength': 2310.0, 'ItemNames': 3, 'ShelfArea': 1760220.0, 'ShelfWidth':
762.0}, {'ShelfLength': 2278.0, 'ItemNames': 1, 'ShelfArea': 759152.0,
'ShelfWidth': 356.0}]]
Can anyone please suggest?
--
https://mail.python.org/mailman/listinfo/python-list
