+ 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.

19th Apr 2020, 10:22 AM
Niloy Malo
Niloy Malo - avatar
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)
19th Apr 2020, 10:51 AM
Oma Falk
Oma Falk - avatar
+ 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)
19th Apr 2020, 10:29 AM
Tibor Santa
Tibor Santa - avatar
+ 2
Niloy Malo you are welcome. But if possible use Tibor Santa s solution. It is always to better to rely on standard solutions.
20th Apr 2020, 7:42 AM
Oma Falk
Oma Falk - avatar
+ 1
Oma Falk the code I posted, works without any import, its builtin
19th Apr 2020, 10:32 AM
Tibor Santa
Tibor Santa - avatar
0
u might split at j and pop last char of first string to sign. In general u should use cmath
19th Apr 2020, 10:26 AM
Oma Falk
Oma Falk - avatar
0
Tibor Santa is it standard or cmath import required?Not too sure
19th Apr 2020, 10:30 AM
Oma Falk
Oma Falk - avatar
0
Tibor Santa I want to split the user defined complex number. But How can I solve this problem?
19th Apr 2020, 10:32 AM
Niloy Malo
Niloy Malo - avatar
0
Tibor Santa If I spilt complex number, I find ("a+", "b"). How can assign the sign char into b?
19th Apr 2020, 10:37 AM
Niloy Malo
Niloy Malo - avatar
0
Thank you Oma Falk
19th Apr 2020, 3:02 PM
Niloy Malo
Niloy Malo - avatar