Pass list of numbers from cartesian to polar

Asked

Viewed 57 times

1

I’m having trouble passing a list of numbers that are in Cartesian coordinates (u,v) and need to convert to polar coordinates. I tried for the package cmath, as follows:

import cmath

cmath.polar(complex(media_v1,media_v2))

the name media_v1 corresponds to variable x, and media_v2 to variable y. Made the following mistake:

TypeError: complex() first argument must be a string or a number, not 'list'

Someone can help me do this with a number list and not just a number, each variable has 360 x values and 360 y values.

1 answer

2


You can use the function zip to align the values of each list and perform coordinate conversion operation with the corresponding values:

media_v1 = [20, 50]
media_v2 = [30, 60]
import cmath
result = [cmath.polar(complex(x, y)) for x, y in zip(media_v1, media_v2)]
print(result)
# [(36.05551275463989, 0.982793723247329), (78.10249675906654, 0.8760580505981934)]
  • I got it, thank you very much. I’m in trouble now because with this code the theta angle is generated in radians and I needed a code that makes generate direct in degrees.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.