Python component for recursive mass addition

Dear community,
what I’m trying to do is to extend this component logic:

to encompass a list or even a data tree structure, so if I feed the component with a list of numbers it will calculate the mass addition of each index as it updates.

I would like to know how to deal with this problem in terms of scripting. I know about ghpythonlib.treehelpers but I kindly ask you if you can provide me some example or tutorials in order to practice a little bit and figure out how to solve my problem.

Thanks a lot.

I don’t know why you need it to be iterative or recursive, so perhaps I don’t understand your goal, but on the actual component (in GH, not in the code), just right click it and set x to List Access. Then in your code just do: a = sum(x).

By the way, pick a different name for the other variable, e.g. list_. list is a built-in and shouldn’t be used like this.

Your code will currently try to apply a class method from the type/class list to x, but either doesn’t have a list instance to apply it to or an item to append (e.g. l = [1,2,3]; list.append(l, 8) works, but that’s poor style compared to calling the instance’s own method).

Hi @James_Parrott and thank you for the reply.
Ok, I will pick a different name for the “list” variable.

About the first part of your answer, I probably didn’t explain well my goal.

I want to feed the component with a list of numbers like: [0,0,0,0] that in the following iteration could be like [1,1,1,1] and then like [1,0,0,1] , and I want the component to compute the sum like this: [2,1,1,2] WITHOUT storing informations inside the component (that’s why I used the “del” method").
Is it more clear now?

Thanks that’s perfectly clear now. It’s very straightforward, I just can’t think how to do it without storing information.

A list of sums depends on every element being summed (every summand) in the lists you give it. So at the very least you need to remember a partial sum, or store it as part of a component’s state.

You don’t have to use scriptcontext.sticky though, you can switch a GhPython component to SDK mode and store a list of partial sums in an instance variable on the component, like so for single numbers:

from numbers import Number

from ghpythonlib.componentbase import executingcomponent as component

class MyComponent(component):
    a = 0
    def RunScript(self, x, reset):
        if reset is True:
            self.a = 0
        elif isinstance(x, Number):
            self.a += x
        return self.a

Otherwise if your lists of numbers always persist in Grasshopper, you can add a new List Access input for each list, and make RunScript accept *args

class MyComponent(component):
    def RunScript(self, *args):
        return [sum(elements) for elements in zip(*args)]

Hi @James_Parrott ,

I tried with your suggestion but it’s not working.
At each iteration it adds to each number of the list a number corresponding to the lenght of the list itself.
So if I have:

[0,0,0,0] the next iteration will be [4,4,4,4] and so on…

The length of all the lists isn’t the same?

Yes, the lenght of the list is always the same, because it’s the same list updated at each iteration.

Thanks. And is the calculation more complicated than adding on the length to every element?


This is the expected behaviour

1 Like

image
Solved!

1 Like