Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Consider the following list

MyList= {-0.0000396, -0.0000187167, "0.10259130990561813-0.0677132394802944i"}

Head /@ MyList (* -> {Real, Real, String} *)

In fact, the String is a complex number.

Is there an easy way of converting MyList to MyNewList such that

Head /@ MyNewList (* -> {Real, Real, Complex} *)
share|improve this question

3 Answers 3

myList = {-0.0000396, -0.0000187167, "0.10259130990561813-0.0677132394802944i"} ;

myNewList = myList/.{x_, y_, z_} :> {x, y, ToExpression@StringReplace[z, "i" -> "I"]}

{-0.0000396, -0.0000187167, 0.102591 - 0.0677132 I}

Head /@ myNewList

{Real, Real, Complex}

share|improve this answer

For a general case, in which the string may be in any position, also this is possible:

MyList /. s_String :> ToExpression[ToUpperCase[s]]

Care should be taken if the numbers come from another program using the common scientific notation, such as 1.0E+7. In this case, the following may help:

myList /. s_String :> ToExpression[StringReplace[ToUpperCase[s], "E" -> "*^"]]
share|improve this answer

A little more general:

myList = {-0.0000396, -0.0000187167, "0.10259130990561813-0.0677132394802944i"}
(ToExpression /@ myList) /. i -> I
Head /@ %
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.