+ 1
How can I spilt complex number in python?
Suppose, cpx=4+j5 here, I want to split numbers 4 and +5 If I spilt cpx by j or +j, I can not find the sign of 5(complex part). How can I solve this problem? Please, help me.
9 Answers
+ 1
cpx="4+j5"
z=cpx.split("j")
z[1]=z[0][-1]+z[1]
z[0]=z[0][:-1]
z=list(map(int,z))
print(z)
+ 5
c = 2 + 3j
print(c)
print(c.real)
print(c.imag)
Also you can define the complex number with constructor and you get the same result:
c = complex(2, 3)
+ 2
Niloy Malo you are welcome.
But if possible use Tibor Santa s solution.
It is always to better to rely on standard solutions.
+ 1
Oma Falk the code I posted, works without any import, its builtin
0
u might split at j and pop last char of first string to sign.
In general u should use cmath
0
Tibor Santa is it standard or cmath import required?Not too sure
0
Tibor Santa I want to split the user defined complex number. But
How can I solve this problem?
0
Tibor Santa If I spilt complex number, I find ("a+", "b"). How can assign the sign char into b?
0
Thank you Oma Falk