Hej,
I have two lists of doubles of equal length. I need to sum the corresponding, and write the values to a new list. It seems like a simple task, but I am getting tripped up.
list a = [1.0 , 2.0 , 3.0 , 4.0 , 5.0]
list b = [2.0 , 5.0 , 4.0 , 2.0 , 1.0]
add the corresponding value to list c. So what I want is:
list c = [3.0 , 7.0 , 7.0 , 6.0 , 6.0]
Unfortunately I am getting:
list c = [1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 2.0 , 5.0 , 4.0 , 2.0 , 1.0]
I’ve written a short Python script to do the job, but I have a semantic error (it works but not as intended). Anybody have a good idea? Here’s the code:
<span style=“font-weight: bold; color: #f92672;”>import</span> clr
clr.<span style=“font-weight: bold; color: #ffffff;”>AddReference</span>(<span style=“color: #e6db74;”>‘ProtoGeometry’</span>)
<span style=“font-weight: bold; color: #f92672;”>from</span> Autodesk.DesignScript.Geometry <span style=“font-weight: bold; color: #f92672;”>import</span> *
<span style=“color: #75715e;”>#The inputs to this node will be stored as a list in the IN variables.</span>
dataEnteringNode = IN[<span style=“color: #ae81ff;”>0</span>]
dataEnteringNode = IN[<span style=“color: #ae81ff;”>1</span>]
<span style=“color: #75715e;”>#Declare variables</span>
a = IN[<span style=“color: #ae81ff;”>0</span>]
b = IN[<span style=“color: #ae81ff;”>1</span>]
<span style=“color: #75715e;”>#Add the respective values of each list</span>
<span style=“color: #75715e;”>#c= [x+y for x,y in zip(a, b)]</span>
<span style=“color: #75715e;”>#c = [a[i] + b[i] for i in range(len(a))]</span>
<span style=“color: #75715e;”>#c = map(lambda x,y:x+y, a, b)</span>
<span style=“color: #75715e;”>#Assign your output to the OUT variable.</span>
OUT = c
As you can see, I have 3 attempts to make it work right ( I have commented them out right now). Perhaps I am missing something obvious.