Group list by the Sum of those list not exceeding the given value

I want to Group the List by those Sum not exceeds the given value(eg:740) irrespective of the list sequence.
see the attached image so that you can understand clearly.

Please Help me out!

Thank you in Advance!

1 Like

try this:

python code:

# Import python library
import sys
pyt_path = r'C:\Program Files (x86)\IronPython 2.7\Lib'
sys.path.append(pyt_path)
import os
import shutil
import math
# Import math library
from math import *

list1 = IN[0]
maxvalue = IN[1]

i,val = 0,0
ind = []

for l in range(0,len(list1)):
	val = val + list1[i]
	if (val>maxvalue):
		val = list1[i]
		ind.append(list1[:i])
		list1 = list1[i:]
		i = 0
	if i == len(list1)-1: 
		ind.append(list1)
        break		
	i = i + 1

#Final output
OUT = ind
2 Likes

Thank you for the Respose @stillgotme

i tried it with your python, but im getting the Empty List with no error.

please see the image below!

convert your list to number first

The bim4struc package has some good bin packing nodes as well if you don’t want to have to worry about maintaining that Python over time.

Thank you @jacob.small for you Help.
The Node " First Fit Decreasing Bin Packing " from BIM4Struc Package has done with my Requirement!

even i got the Indices, Thank you.

Have a good day!

2 Likes

Hi, Not sure what I’m doing wrong with this?

you will need to add another nested for loop if you wanna do multiple list item. From your @L2@L1, i think there is multiple list right?

EDIT: i just did a new code that works on dynamo 2.8:

# Import python library
import sys
pyt_path = r'C:\Program Files (x86)\IronPython 2.7\Lib'
sys.path.append(pyt_path)
import os
import shutil
import math
# Import math library
from math import *

def SplitByMaxNumber(listItems, maxValue):
	splittedList = []
	sum = 0
	tempList = []	
	count = 0
	for val in list1:	
		if sum>maxvalue:		
			sum = 0
			splittedList.append(tempList)
			tempList = []
		else:
			sum = sum + val
			tempList.append(val)
			
		if count == len(list1) -1:		
			splittedList.append(tempList)			
		count = count + 1
	return splittedList
		
list1 = IN[0]
maxvalue = IN[1]

#Final output
OUT = SplitByMaxNumber(list1, maxvalue)
1 Like

Awesome I think its working! thank you!

hmmm seems to be out putting more values then go in?

hi there, changed the code a little because of duplicates. it should suit your needs now

1 Like

very kind thank you