How to add / multiply numbers in 2 list with different length with Python?


#1
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

import clr
clr.AddReference(“RevitAPI”)
from Autodesk.Revit.DB import *

clr.AddReference(“RevitNodes”)
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)

clr.AddReference(“RevitServices”)
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

line = IN[0]
elements = IN[1]

test = ()
a = ()
b = ()
c = ()

for i in (line):
for e in (elements):
a = ((i)+(e))

b = [i*j for i,j in zip(line,elements)]

for i,j in zip(line,elements):
c = (i*j)

OUT = a, b, c


#2

I want to multiply the numbers in 2 list
i managed to get i don. But i aspected in list 1 on possition 4 a 0
is there a way to get a 0?

and is there another way to write the following line and get the same result?:
b = [i+j for i,j in zip(line,elements)]


#3

Sorry, I am confused with what you want as the outcome. If all you are trying is to get a and c to look like b then it is below.

b = [i+j for i,j in zip(line,elements)] is the most concise way to write that that I can think of. Your c is only different because you keep making c = whatever the current step is, so only the last one gets saved and outputted. Same as a.

If you wanted to make them like your b, write it like this:

c = []
for i,j in zip(line,elements):
    c.append(i*j)

Are you trying to get the equivalent of lacing done in python?


#4

Thanks Kenny
this was almost exactly what i was looking for.
i knew there most be a way to write it in the following way:

for i,j in zip(line,elements):
c.append(i*j)

but i put in front of it:
c = ()

in 1 list i got the numbers: 1 4 9 16. But because list IN[0] is shorter i want that the last one off list IN[1] wil be multiplied by 0

the outcome must be 1, 4, 9, 16, 0

How lacing is done in Python?


#5

As far as I know, if list lengths are different, you have to manually add it in into the code. For your example, you want the shorter list to be populated with 0’s to the length of the longer list.

Here is an example python script that adds 0 to whichever list is shorter:

if len(IN[0])<len(IN[1]):
  c = len(IN[1])-len(IN[0])
  for i in range(c):
    IN[0].append(0)
elif len(IN[0])>len(IN[1]):
  c = len(IN[0])-len(IN[1])
  for i in range(c):
    IN[1].append(0)

prod = []
for i,j in zip(IN[0],IN[1]):
	prod.append(i*j)

OUT = prod

This version allows for a third input which you can customize to add to the shorter list (example I used was 50, so 50 * 5 = 250):

if len(IN[0])<len(IN[1]):
  c = len(IN[1])-len(IN[0])
  for i in range(c):
    IN[0].append(IN[2])
elif len(IN[0])>len(IN[1]):
  c = len(IN[0])-len(IN[1])
  for i in range(c):
    IN[1].append(IN[2])

prod = []
for i,j in zip(IN[0],IN[1]):
	prod.append(i*j)

OUT = prod


#6

Thanks a lot Kenny

this is what i want :+1: