14
$\begingroup$

I got the following problem I don't know how to solve or if it's at all possible to solve(I'm quite new to Mathematica and don't have an IT background, so please explain slowly if possible:))

I'm trying to convert a simple string which I imported from a homepage into a list of numbers. The string looks for example the following:

{"1,2,3,5,10,12,13,17,26,30,32,41,42,43,113,115,121,125"}

Mathematica sees this as one string, this is important, not as a list of strings. (I used the head function to check that)

My question: is it possible to convert this one string into a list of numbers so that each number in the string above is recognized as an individual element and can be calculated with?

Another important note: these strings don't all contain the same amount of numbers, a couple of them are shorter than other ones.

Is there any way I can convert these different strings into lists of numbers?

$\endgroup$

6 Answers 6

25
$\begingroup$
str = {"1,2,3,5,10,12,13,17,26,30,32,41,42,43,113,115,121,125"}

Flatten@ToExpression@StringSplit[str, ","]

Short explanation:

After executing StringSplit you get a list of separated "StringNumbers" like

{{"1", "2", ... "125"}}

ToExpression converts these "StringNumbers" to Integers. Flatten removes the outermost brackets. You can even omit Flatten by looking at gpap's comment. This works for lists of varying lengths and also for different number types.

EDIT

To also answer the question in your comment:

eq = {{"1", "3", "5", "6"}, {"1", "2", "4", "7"}, {"1", "3"}};

ToExpression[Flatten /@ Map[StringSplit[#, ","] &, eq]]
$\endgroup$
3
  • 1
    $\begingroup$ beat me to it! You can use the shorter ToExpression@@StringSplit[str,","] $\endgroup$
    – gpap
    Commented Jun 9, 2014 at 9:36
  • $\begingroup$ This works perfect, thanks for the very quick answering guys:) I have one more question regarding the same subject: I have a list of these strings that looks the following: eq={{1,3,5,6},{1,2,4,7,10,11},{1,3,4,5,7}}. Is it somehow possible to convert these strings into numbers all at once or do I have to convert each string in the eq-list separately?:) $\endgroup$
    – Phily
    Commented Jun 9, 2014 at 9:43
  • $\begingroup$ upvote for explanation (however simple it must be to someone with 12.6k creds.) Too often all one gets is something incomprehensible to all but Mathematica-experts. $\endgroup$
    – Paul_A
    Commented Sep 17, 2015 at 4:39
11
$\begingroup$

I would use ImportString myself:

string = "1,2,3,5,10,12,13,17,26,30,32,41,42,43,113,115,121,125";

ImportString[string, "CSV"]
{{1, 2, 3, 5, 10, 12, 13, 17, 26, 30, 32, 41, 42, 43, 113, 115, 121, 125}}

Are you using Mathematica for the operation which I imported from a homepage? There may be a more direct method in that case.


Regarding your comment:

I have a list of these strings that looks the following: eq={{1,3,5,6},{1,2,4,7,10,11},{1,3,4,5,7}}. Is it somehow possible to convert these strings into numbers all at once or do I have to convert each string in the eq-list separately?:)

The fastest way I am aware of uses an internal function I described here. It is not error-tolerant so make sure your data is as you describe or it may crash the Kernel. A quick example of use:

eq = {{"1", "3", "5", "6"}, {"1", "2", "4", "7", "10", "11"}, {"1", "3", "4", "5", "7"}};

System`Convert`TableDump`ParseTable[eq, {{{}, {}}, {"-", "+"}, "."}, False]
{{1, 3, 5, 6}, {1, 2, 4, 7, 10, 11}, {1, 3, 4, 5, 7}}

See the linked answer for an explanation of the parameters.


If you prefer not to use the error-intolerant and undocumented ParseTable you can as Chip Hurst reminds make direct use of ToExpression:

ToExpression @ eq
{{1, 3, 5, 6}, {1, 2, 4, 7, 10, 11}, {1, 3, 4, 5, 7}}

This is however more than an order of magnitude slower than ParseTable.

$\endgroup$
4
  • 1
    $\begingroup$ For your second part, can't you just do ToExpression[eq]? $\endgroup$
    – Greg Hurst
    Commented Aug 12, 2014 at 15:14
  • 2
    $\begingroup$ @ChipHurst Yes you can, but as I said it is not "the fastest way" which is what I was attempting to offer. ParseTable is more than an order of magnitude faster on this kind of data. (v10 timings.) $\endgroup$
    – Mr.Wizard
    Commented Aug 12, 2014 at 17:43
  • $\begingroup$ Got it, and yes it's touchy, my kernel crashed too. :-) $\endgroup$
    – Greg Hurst
    Commented Aug 12, 2014 at 17:50
  • 1
    $\begingroup$ @ChipHurst I incorporated your implicit suggestion into this answer, with credit of course. $\endgroup$
    – Mr.Wizard
    Commented Aug 12, 2014 at 17:56
6
$\begingroup$

An alternative method:

list = {"1,2,3,5,10,12,13,17,26,30,32,41,42,43,113,115,121,125"};

StringCases[#, x : NumberString :> ToExpression @ x] & /@  list
{{1, 2, 3, 5, 10, 12, 13, 17, 26, 30, 32, 41, 42, 43, 113, 115, 121, 125}}

Or

ToExpression["{" <> # <> "}"] & /@ list
$\endgroup$
2
  • $\begingroup$ Even playing with Trace I couldn't figure out how your last one works. But it works. $\endgroup$
    – eldo
    Commented Jun 9, 2014 at 10:17
  • $\begingroup$ @eldo well, this is ugly strightforward way: "1, 2" -> "{1, 2}" -> {1, 2} $\endgroup$
    – Kuba
    Commented Jun 9, 2014 at 10:26
5
$\begingroup$

With V10 one can also do

str = "4.5e2, 0.08, 5, 10";

Interpreter[DelimitedSequence["Number"]][str] // Flatten

{450., 0.08, 5, 10}

Or even

str = "4.5e2, 0.08, 5, 10, one, one divided by forty-eight";

Interpreter[DelimitedSequence["SemanticExpression"]][str] // Flatten //InputForm

{450., 0.08, 5, 10, 1, 1/48}

$\endgroup$
1
  • $\begingroup$ Interpreter can be very slow; SemanticImportString may be a better choice. See: (57865) $\endgroup$
    – Mr.Wizard
    Commented Sep 15, 2014 at 1:48
4
$\begingroup$

For completeness sake, another solution:

str = "1,2,3,5,10,12,13,17,26,30,32,41,42,43,113,115,121,125";
FromDigits /@ StringSplit[str, ","]
$\endgroup$
1
$\begingroup$
str={"1,2,3,5,10,12,13,17,26,30,32,41,42,43,113,115,121,125"}

ToExpression[StringCases[str[[1]], DigitCharacter ..]]

ToExpression[StringSplit[str[[1]], ","]]

ToExpression["{" ~~ str[[1]] ~~ "}"]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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