Home > Software design >  How do I run an array list through the equation y = 3x 2 [duplicate]
How do I run an array list through the equation y = 3x 2 [duplicate]

Time:09-17

I have a lab where in one part I need to use the array:

A = [3, 3, 20, 8, -2, 1, 3, -7, 2, 1, 3, -5, 13]

and I have to run all the elements of that array in this equation Y = 3x 2, the x will be the elements of the array A and all the elements need to be used there, but I don't know how to make this. Also, it needs to be in a function with the name Ecuacion, e.g.: def Ecuacion():`

Edit: Then I need to print the elements I got from the equation.

CodePudding user response:

A simple for loop should do the trick:

for x in A:
    y = 3 * x   2
    print(y)

CodePudding user response:

Is this what you want?

def func(A):
    B = list(map(lambda x: x * 3   2, A))
    return B

A = [3, 3, 20, 8, -2, 1, 3, -7, 2, 1, 3, -5, 13]
print(func(A))

CodePudding user response:

That should work

def ecuacion(x):
    B = []
    for i in x:
        y = 3*i   2
        B.append(y)
    return B

A = [3, 3, 20, 8, -2, 1, 3, -7, 2, 1, 3, -5, 13]
print(ecuacion(A))

Output:

[11, 11, 62, 26, -4, 5, 11, -19, 8, 5, 11, -13, 41]
  • Related