I am trying to calculate a resultant acceleration from x, y, z accelerations. I have written a function that does this when manually inputting the x,y,z coordinates.
def Euclidean_norm(x,y,z):
total = (x ** 2) + (y ** 2) + (z ** 2)
resultant = math.sqrt(total)
return resultant
My problem is that I want to use this function to iterate over 3 lists, and produce a new list with only the resultant acceleration.
x_list = [(9.6,), (4.9,), (8.7,), (9.....]
y_list = [(0.6,), (2.6,), (4.6,), (2.....]
z_list = [(5.2,), (7.2,), (5.8,), (7.....]
I have tried to use the map function
print(map(Euclidean_norm(a,b,c)))
However this gave an error
TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'
Part of the problem is that the values for x
, y
and z
I get from the database come out as tuples instead of plain numbers.
Use a list-comprehension with zip()
:
[Euclidean_norm(*x,*y,*z) for x,y,z in zip(x_list, y_list, z_list)]
Oh, and you shouldn't capitalise functions as capitalised names are reserved for classes.
Just a little explanation...
The zip()
function takes any number of iterables and "zips" them together to give tuples made from the corresponding indexes. It is a really useful function for iterating over related lists like the ones here.
To demonstrate it a bit clearer:
>>> a = [1, 2, 3]
>>> b = [2, 4, 6]
>>> list(zip(a, b))
[(1, 2), (2, 4), (3, 6)]
>>> for i, j in zip(a, b):
... print(i, j)
...
1 2
2 4
3 6