Python has a way to solve complex numbers; it actually has a type for them ( <class 'complex'>
). As a result, and to avoid having to reinvent the wheel, I highly recommend using this.
, ( strings
complex
). Python , 'j'
, 'i'
.
l1 = ['13+i', '1+4i', '3+2i', '11+4i', '5+i', '10+2i', '5+4i']
l1 = [complex(x.replace('i', 'j')) for x in l1]
print(l1)
, , .real
.imag
complex
list-compehensions.
real_parts = [value.real for value in l1]
print(real_parts)
# -> [13.0, 1.0, 3.0, 11.0, 5.0, 10.0, 5.0]
imaginary_parts = [value.imag for value in l1]
print(imaginary_parts)
# -> [1.0, 4.0, 2.0, 4.0, 1.0, 2.0, 4.0]
, (, ). [int(value.real) for value in l1]
int
.
, - , , , , , , , . , 4j
( ) 1-j
( , .split('+')[0]
) ..