2
$\begingroup$

I have a large list of lists that contains mixed data, some real, some strings. The lists are of various lengths and depths. Some of the real data values have an asterisk appended to them, e.g.:

{14.2, 14.7, 15.0, {2.4*, 3.5, 4.4*, {2.9, 10.0}}, ...}

What is an easy way to remove the asterisks?

Since there seems to be some confusion about what I am asking, I'm adding this background. The data is imported from an external file, using Mathematica's Import[] function. In that file, some of the data values have asterisks next to them. The result of the import is a list of lists like I have written above. The data values without an asterisk are interpreted as real, while the values with an asterisk are interpreted as strings. I want to get rid of the asterisks and recover the corresponding data values as real values.

$\endgroup$
  • 5
    $\begingroup$ real data values have an asterisk appended to them how can a number has an asterisk appended to them? You mean it is a string representation of a number? So is it like "4.4*"? Or did you mean 4.4"*" ? but this makes no sense, it is multiplying 4.4 by string "*". How about posting, in Mathematica code, a small example of the list? $\endgroup$ – Nasser Mar 5 '15 at 2:21
1
$\begingroup$

The data values without an asterisk are interpreted as real, while the values with an asterisk are interpreted as strings.

Okay, so the actual format of your imported data is apparently:

dat = {14.2, 14.7, 15.0, {"2.4*", 3.5, "4.4*", {2.9, 10.0}}}

A solution:

fix =
  s_String?(StringMatchQ[#, "*\\*"] &) :> Internal`StringToDouble @ StringDrop[s, -1];

dat /. fix
{14.2, 14.7, 15., {2.4, 3.5, 4.4, {2.9, 10.}}}
$\endgroup$
  • $\begingroup$ Well, the quotation marks you put in above do not appear in my data, but I can verify that Mathematica interprets those values as strings by using the Head[] function. Thank you for the solution. $\endgroup$ – kevinea Mar 8 '15 at 16:38
  • $\begingroup$ @kevinea Right, when I wrote "imported" I meant "after using Import" as opposed to the original format of your data before Mathematica touched it. Thanks for the Accept. $\endgroup$ – Mr.Wizard Mar 9 '15 at 0:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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