separate real and imaginary part of a complex number in python

亡梦爱人 提交于 2019-12-21 06:58:44

问题


I have need to extract the real and imaginary elements of a complex number in python. I know how to make a list into a complex number... but not the other way around.

I have this:

Y = (-5.79829066331+4.55640490659j)

I need:

Z = (-5.79829066331, 4.55640490659)

and I will also need each part if there is a way to go directly without going by way of Z:

A = -5.79829066331
B = 4.55640490659

https://docs.python.org/2/library/functions.html#complex

Thanks!


回答1:


Y = (-5.79829066331+4.55640490659j)

Z = (Y.real, Y.imag)

A = Y.real
B = Y.imag



回答2:


Z = (Y.real, Y.imag)
A = Y.real
B = Y.imag



回答3:


   import numpy as np                 #Can be done easily using Numpy Lib
   array=np.array([3,4.5,3 + 5j,0])   #Initialize complex array
   real=np.isreal(array)              #Boolean condition for real part
   real_array=array[real]             #Storing it in variable using boolean indexing
   imag=np.iscomplex(array)           #Boolean condition for real part
   imag_array=array[imag]             #Storing it in variable using boolean indexing
   print(real)
   print(imag)
   print(real_array)
   print(imag_array)


来源:https://stackoverflow.com/questions/24592803/separate-real-and-imaginary-part-of-a-complex-number-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!