Help with counting iteration in Python

Hi,
I’m trying to write a script that iterates through a list of numbers, adding them until they reach a maximum, and then printing the lists of values. I have a list of four numbers that can be used and a Target number for the max limit:

import Rhino.LengthValue as rl
import Rhino.Geometry as rg
import rhinoscriptsyntax as rs
import System.Double
import clr
clr.AddReference("Grasshopper")
from Grasshopper.Kernel.Data import GH_Path
from Grasshopper import DataTree


def listspecs(List):
    Value = 0
    for n in List:
    Value += n
    MassValue[:] = []
    MassValue.append(Value)
    ListLength[:] = []
    ListLength.append(len(List)) 

def iterate(i, j, SizeList):
    for size in Sizes:
        
        listspecs(SizeList)
        if MassValue <= Target:
            SizeList.append(size)
            iterate(i, j, SizeList)
        else:
            j += 1
            MasterSizeList.AddRange(SizeList, GH_Path(i,j))

def start(i, j, SizeList):
     for size in Sizes:
    #MassValue = size
    i += 1
    #j += 1
    SizeList.append(size)
    iterate(i, j, SizeList)

MasterSizeList = DataTree[System.Double]()
i = -1
ListLength = []
MassValue = []
SizeList = []
j = 0
start(i, j, SizeList)

The results that I’m Expecting are lists like:
(0.337, 0.337), (0.337, 0.362),…(0.362, 0.412),…(0.412, 0.412).
What I’m trying to do in the script is start with a number, add a number, test if it’s less than the target (0.9) and if it is then add another number and if it isn’t print the list of values without the last number that makes it go over the Target. Any advice or hints as to why my script isn’t working?
Thanks.

Hello,

To get your variables back out of the functions you need to return something, eg return SizeList and assign the returned variable to something, eg SizeList = start(i, j, SizeList)

the script looks dense to me for what you describe. Is this the output you’re after?


list-max-target.gh (7.0 KB)

Better not use list as a variable name, as it is also a keyword in Python. You’re now hiding the original list

ah, thanks. Is that also problematic when using it as an argument like I did?

I didn’t have coffee yet it seems, because I don’t need to send that list to the function:

1 Like