我需要在python中提取复数的实数和虚数元素。我知道如何将列表列入复数...但不是相反。
我有这个:
Y = (-5.79829066331+4.55640490659j)
我需要:
Z = (-5.79829066331, 4.55640490659)
我还需要每个部分,如果有办法直接去Z而不是Z:
A = -5.79829066331
B = 4.55640490659
https://docs.python.org/2/library/functions.html#complex
谢谢!
答案 0 :(得分:21)
Y = (-5.79829066331+4.55640490659j)
Z = (Y.real, Y.imag)
A = Y.real
B = Y.imag
答案 1 :(得分:10)
Z = (Y.real, Y.imag)
A = Y.real
B = Y.imag
答案 2 :(得分:0)
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)