Interation in python?

Hello,

I try to learn more complex structures. But i still struggle…

def quad(x):
erg = x * x
return erg

# Funktion mit mehr als einem Parameter
def summe(a,b,c):
erg = a + b + c
return erg

# Funktion mit einem Parameter mehrmals aufrufen
z = map(quad, [4, 2.5, -1.5])

# Jedes Ergebnis ausgeben
x = []
for i in z:
     x.append(i)


# Funktion mit mehr als einem Parameter mehrmals aufrufen
z = map(summe, [3, 1.2, 2], [4.8, 2], [5, 0.1, 9])

# Jedes Ergebnis ausgeben
y = []
for a in z:
     y.append(a)

OUT = x,y

Can i write like this? Is there a missing library for these buildIn functions?

KR

Andreas

@Draxl_Andreas
at the line z = map(summe, [3, 1.2, 2], [4.8, 2], [5, 0.1, 9])
lists are not uniform Python trying to add 2 + None + 9

an alternative

def quad(x):
	erg = x * x
	return erg

def summe(*args):
	erg = sum(x for x in args if x is not None)
	return erg

z = map(quad, [4, 2.5, -1.5])

x = []
for i in z:
	 x.append(i)


z = map(summe, [3, 1.2, 2], [4.8, 2], [5, 0.1, 9])

y = []
for a in z:
	 y.append(a)

OUT = x,y
1 Like

…you mean i have to replace the addition (sum) with a ForLoop. thats the only way to sum up elements - instead i have to “collect” them! :slight_smile:

x for x in args if x is not None # writing - style ?

I don`t know this writing style, where do you get this kind of “coding” is there also a link for optimising codewriting in python?

this is a solution the goal is to check if there is a None value before making the addition

it’s a List comprehension

1 Like