Add corresponding values of two equal length lists


#1

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.

 


#2

sorry about all the color style nonsense. Here’s the code again as something more readable.
import clr
clr.AddReference(‘ProtoGeometry’)
from Autodesk.DesignScript.Geometry import *
#The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN[0]
dataEnteringNode = IN[1]

#Declare variables
a = IN[0]
b = IN[1]

#Add the respective values of each list
#c= [x+y for x,y in zip(a, b)]
#c = [a[i] + b[i] for i in range(len(a))]
#c = map(lambda x,y:x+y, a, b)
#Assign your output to the OUT variable.
OUT = c

You can see my 3 attempts (that don’t work)!


#3

Okay fixed my own problem! (hooray) It was a dynamo issue rather than a python issue.

 

Element-wise


#4

It’s indeed very simple and requires no Python:

Capture