Separate a list based on sum of value

Hello.
I’m trying to automate detail insertion using detail sizes. Below in code block “A” is a list of detail sizes from an excel spread sheet. I need the detail sizes in code block “A” separated as shown in code block “B” in order to insert the details into each sheet properly. Each row of details can only have a maximum sum of 4 and each sheet has 2 rows of details.

I tried to use Math.Sum to add up all the values in code block “A”, then divide by 4, rounded the results down to give me a whole number to group them together per row of details but the details tend to overlap either at the end or beginning of each row of details.

Happened to have this saved. There are some differences, namely that the example has fixed bins and aimed to have small overflow (versus your bin packing), but it should be a good reference.

1 Like

@lyle See if this helps:

splitList,subList = [],[]

for i in IN[0]:
	if sum(subList,i) <= IN[1]:
		subList.append(i)
	else:
		splitList.append(subList)
		subList = [i]
splitList.append(subList)

OUT = splitList
2 Likes

Thank you AmolShah for the python script, it’s exactly what I was looking for.

Robert_Younger, I appreciate the help as well.

1 Like