Hello,
how can i interate with a lambda expresion thrue a list?
import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
mal = lambda x,y: x*y
plus = lambda x,y: x+y
OUT = mal(IN[0],IN[1]),plus(IN[0],IN[1])
KR
Andreas
@Draxl_Andreas You need to induce a for loop to iterate through the list.
See if this helps you understand.
mal = lambda x,y: x*y
plus = lambda x,y: x+y
#To make sure code works with 1 number and also a list of numbers
toList = lambda x : x if hasattr(x, '__iter__') else [x]
#To store the results
mul = []
add = []
#To iterate through all values in a list
for num1,num2 in zip(toList(IN[0]),toList(IN[1])):
mul.append(mal(num1,num2))
add.append(plus(num1,num2))
OUT = mul,add
1 Like
thank you! what is ββiterββ in that case a buildinfuction ?
@Draxl_Andreas
Iterator (iter) in Python is simply an object that can be iterated upon. An object which will return data, one element at a time. You need an iterator while implementing a for loop or it will throw error.
So what we are doing is essentially checking if the input has an attribute iter
(in simplest form: it helps to checks if the input is a part of some sort of list from which the values can be called one after another)
Alternatively, you can use something like
toList = lambda x : x if isinstance(x,list) else [x]
But it is recommended using the hasattr(x, βiterβ) since it is more robust.
1 Like