13

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?

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Phily
  • 171
  • 1
  • 1
  • 7

6 Answers6

24
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]]
eldo
  • 67,911
  • 5
  • 60
  • 168
  • 1
    beat me to it! You can use the shorter ToExpression@@StringSplit[str,","] – gpap Jun 09 '14 at 09: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 09 '14 at 09:43
  • 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 04:39
11

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.

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
6

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
Kuba
  • 136,707
  • 13
  • 279
  • 740
5

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}

eldo
  • 67,911
  • 5
  • 60
  • 168
4

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, ","]
sakra
  • 5,120
  • 21
  • 33
1
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]] ~~ "}"]
Basheer Algohi
  • 19,917
  • 1
  • 31
  • 78