Grouping of items based on criteria
<p>I have a list of items:</p>

<pre><code>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}]
</code></pre>

<p>I need the program that tells how many minimum number of groups one can have and how the items are grouped.</p>

<p>I would like to form groups of these items such that sum of shelflength of items &lt;= max length or sum of shelfwidth of items  &lt;= max width and sum of ShelfArea of items &lt;= 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 &amp; 4 will form other group. I would like to have the answer in the format:</p>

<pre><code>[[{'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}]]
</code></pre>

<p>I have written a code but it does not give the result I wanted.</p>

<pre><code>ShelvesToPack_sorted = sorted(ShelvesToPack, key = itemgetter('ShelfWidth'), reverse = True)
AreaOfObject = 2972897.28
current_width = 0
current_length = 0
current_area = 0
ply =[]
plywoods=[]
for item in ShelvesToPack_sorted:
    if (current_width + item['ShelfWidth'] &lt;= 1219.2  or  current_length + item['ShelfLength'] &lt;= 2438.5) and  current_area + item['ShelfArea'] &lt;= AreaOfObject:
        ply.append(item)
        current_width += item['ShelfWidth']
        current_length += item['ShelfLength']
        current_area += item['ShelfArea']
    else:
       plywoods.append(ply)
       if (item['ShelfWidth'] &lt;= 1219.2  or item['ShelfLength'] &lt;= 2438.5) and item['ShelfArea'] &lt;= AreaOfObject:
           ply = [item]
           current_width = item['ShelfWidth']
           current_length = item['ShelfLength']
           current_area = item['ShelfArea']
       else:
           ply = []
           current_width = 0
           current_length = 0
           current_area = 0

if ply:
    plywoods.append(ply)

print(plywoods)
</code></pre>

<p>I have got the following output which is not quite right and I am unable to do the correct grouping.</p>

<pre><code>[[{'ItemNames': 3, 'ShelfWidth': 762.0, 'ShelfLength': 310.0, 'ShelfArea': 1760220.0}],
 [{'ItemNames': 2, 'ShelfWidth': 610.0, 'ShelfLength': 1220.0, 'ShelfArea': 372100.0},
  {'ItemNames': 4, 'ShelfWidth': 610.0, 'ShelfLength': 610.0, 'ShelfArea': 1450435.0}],
 [{'ItemNames': 1, 'ShelfWidth': 356.0, 'ShelfLength': 2278.0, 'ShelfArea': 759152.0}]]
</code></pre>

<p>Can anyone please suggest?</p>

<p>Here is a simplified version of your code:</p>

<pre><code>data = [{'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}]
SL = 'ShelfLength'
SW = 'ShelfWidth'
SA = 'ShelfArea'
IN = 'ItemNames'
max_width = 1219.2
max_len = 2438.5
max_area = 2972897.28

grouped_data = [[], []]
for record in data:
    if (record[SL] &lt;= max_len or record[SW] &lt;= max_width) and record[SA] &lt;= max_area:
        grouped_data[0].append(record)
    else:
        grouped_data[1].append(record)

print(grouped_data)
</code></pre>

<p>This gives the same result you got, which is correct given that all elements satisfy the condition you mentioned.</p>

python python-3.x