U
U
Ulyana Illiterate2016-10-24 16:13:52
Python
Ulyana Illiterate, 2016-10-24 16:13:52

How to convert a list of numbers to a list of complex numbers in Python?

Given a list for example: data=[4,5,7,8,9,0,3,3,6,6]
it contains 5 complex numbers (re1,im1,re2,im2,re3,im3....)
how to make from this list, a list with complex numbers?
something like this data=[(4+5j),(7+8j),...]

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andy_U, 2016-10-24
@karulyana

data=[4, 5, 7, 8, 9, 0, 3, 3, 6, 6]
c = [complex(a, b) for a, b in zip(data[:-1:2], data[1::2])]
print(c)

S
sts, 2016-10-24
@stunoff

this is how it should be:

data=[4,5,7,8,9,0,3,3,6,6]
new = []
it = iter(data)

for x in it:
    new.append(complex(float(x), float(next(it))))

print(new)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question