pheon pheon - 2 months ago 4
Python Question

How to convert the string '1.000,0.001' to the complex number (1+0.001j)?

The best I could come up with is

s = '1.000,0.001'
z = [float(w) for w in s.split(',')]
x = complex(z[0],z[1])


Is there a shorter, cleaner, nicer way?

Answer

There's a more concise way, but it's not really any cleaner and it's certainly not clearer.

x = complex(*[float(w) for w in '1.000,.001'.split(',')])