在python中分离复数的实部和虚部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24592803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
separate real and imaginary part of a complex number in python
提问by litepresence
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.
我需要在python中提取复数的实部和虚部。我知道如何将列表变成一个复数……但反过来不行。
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:
如果有一种方法可以直接去而不通过Z,我也需要每个部分:
A = -5.79829066331
B = 4.55640490659
https://docs.python.org/2/library/functions.html#complex
https://docs.python.org/2/library/functions.html#complex
Thanks!
谢谢!
回答by Phonon
Z = (Y.real, Y.imag)
A = Y.real
B = Y.imag
回答by furas
Y = (-5.79829066331+4.55640490659j)
Z = (Y.real, Y.imag)
A = Y.real
B = Y.imag
回答by Harshal SG
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)