pheon pheon - 1 year ago 59
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 Source

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(',')])
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download