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.

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?

share|improve this question

6 Answers 6

up vote 14 down vote accepted
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]]
share|improve this answer
1  
beat me to it! You can use the shorter ToExpression@@StringSplit[str,","] –  gpap Jun 9 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 at 9:43
    
+1 - enlightened, congrats! –  Artes Sep 5 at 19:38

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.

share|improve this answer
1  
For your second part, can't you just do ToExpression[eq]? –  Chip Hurst Aug 12 at 15:14
2  
@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 at 17:43
    
Got it, and yes it's touchy, my kernel crashed too. :-) –  Chip Hurst Aug 12 at 17:50
1  
@ChipHurst I incorporated your implicit suggestion into this answer, with credit of course. –  Mr.Wizard Aug 12 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
share|improve this answer
    
Even playing with Trace I couldn't figure out how your last one works. But it works. –  eldo Jun 9 at 10:17
    
@eldo well, this is ugly strightforward way: "1, 2" -> "{1, 2}" -> {1, 2} –  Kuba Jun 9 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}

share|improve this answer
    
Interpreter can be very slow; SemanticImportString may be a better choice. See: (57865) –  Mr.Wizard Sep 15 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, ","]
share|improve this answer
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]] ~~ "}"]
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.