# Convert a string into a list of numbers

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?

-

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

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

ToExpression[Flatten /@ Map[StringSplit[#, ","] &, eq]]

-
beat me to it! You can use the shorter ToExpression@@StringSplit[str,","] – gpap Jun 9 '14 at 9:36
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?:) – Phily Jun 9 '14 at 9:43
+1 - enlightened, congrats! – Artes Sep 5 '14 at 19:38
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. – Paul_A Sep 17 '15 at 4:39

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.

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"}};

SystemConvertTableDumpParseTable[eq, {{{}, {}}, {"-", "+"}, "."}, False]

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


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.

-
For your second part, can't you just do ToExpression[eq]? – Chip Hurst Aug 12 '14 at 15:14
@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.) – Mr.Wizard Aug 12 '14 at 17:43
Got it, and yes it's touchy, my kernel crashed too. :-) – Chip Hurst Aug 12 '14 at 17:50
@ChipHurst I incorporated your implicit suggestion into this answer, with credit of course. – Mr.Wizard Aug 12 '14 at 17:56

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

-
Even playing with Trace I couldn't figure out how your last one works. But it works. – eldo Jun 9 '14 at 10:17
@eldo well, this is ugly strightforward way: "1, 2" -> "{1, 2}" -> {1, 2} – Kuba Jun 9 '14 at 10:26

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}

-
Interpreter can be very slow; SemanticImportString may be a better choice. See: (57865) – Mr.Wizard Sep 15 '14 at 1:48

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, ","]

-
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]] ~~ "}"]
`
-