I have a huge string database with the form such as {{position},{1,2,3,4,5,..}}, and I want to convert such database to Interger data in a very quick way.
In the following example, I created a string database (instead of my real simulation database).
StringData = {};
steps=5000; (* for testing, in real simulation it is large*)
Do[AppendTo[StringData, {"Position"<>ToString[ii],"0,1,1,1,22,1,2,14,5,2,2,1,5,"}], {ii, 1, steps}];
strtolist = ConstantArray[{}, Length[StringData]];
For[ii = 1, ii <= Length[StringData], ii++,
strtolist[[ii]] = ToExpression[StringSplit[StringData[[ii]][[2]], ","]];
]; // AbsoluteTiming
strtolist = ConstantArray[{}, Length[StringData]];
For[ii = 1, ii <= Length[StringData], ii++,
strtolist[[ii]] = IntegerPart/@Internal`StringToDouble/@StringSplit[StringData[[ii]][[2]], ","];
]; // AbsoluteTiming
{0.248431, Null}
{0.100303, Null}
The second way is much fast. My real simulation is a large database and I wonder whether there are even quicker way to do such converting? for example without doing the outside for-loop? Thank you very much!
one additional problem using
Internal`StringToDouble:when the number is very large as the following example
test = {"0", "33837677493872221", "311462297063636041906"};
numstr1 = IntegerPart /@ Internal`StringToDouble /@ test
numstr2 = IntegerPart /@ Internal`StringToDouble /@ test[[3]]
the results:
{0, 33837677493872220, IntegerPart[$Failed["Bignum"]]}
311462297063636041906
Why numstr2 works good while numstr1 doesn't work? It seems Internal`StringToDouble works fine with single string not string lists?
What if the StringData contains number like "-1","-2" and so on? Only thinking about Integer number (including negative and positive). Is there any other way to do the same work instead of using
ToExpression?
Map[FromDigits, StringSplit[StringData[[All, 2]], ","], {-1}]? – kglr Aug 30 '19 at 20:42Internal`StringToDoubledoes not work with with a lists containing large number but fine with one single string. Do you know why?@kglr – Xuemei Aug 30 '19 at 21:00Map[FromDigits, StringSplit[StringData[[All, 2]], ","], {-1}]works good. Is it possible works with string list with negitave number such as "-1" and so on? @kglr – Xuemei Aug 30 '19 at 21:07Internal`StringToDoubledoes not have theListableattribute. You can make listable version usingistd = Internal`StringToDouble; SetAttributes[istd, Listable]– kglr Aug 30 '19 at 21:08Listable... I hadn't realized it would take precedence over evaluating toInternal`StringToDouble. I assumed theHeadwould evaluate, then anyAttributeswould apply but I suppose it's the other way around? – b3m2a1 Aug 30 '19 at 21:17numstr2is still a string because you mapped on a string not a list. – Chris Degnen Aug 30 '19 at 22:16SetAttributes[istd, Listable], the problems still exists. – Xuemei Aug 30 '19 at 22:21istdas:istd[x_]:= Internal`StringToDouble[x]; SetAttributes[istd, Listable];and use it asIntegerPart @ istd @ StringSplit[StringData[[All, 2]], ","]( but this is slower than mappingInternal`StringToDoubleat level{-1}and usingIntegerParton the entire list. – kglr Aug 31 '19 at 00:57