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}]
? $\endgroup$Internal`StringToDouble
does not work with with a lists containing large number but fine with one single string. Do you know why?@kglr $\endgroup$Map[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 $\endgroup$Internal`StringToDouble
does not have theListable
attribute. You can make listable version usingistd = Internal`StringToDouble; SetAttributes[istd, Listable]
$\endgroup$Listable
... I hadn't realized it would take precedence over evaluating toInternal`StringToDouble
. I assumed theHead
would evaluate, then anyAttributes
would apply but I suppose it's the other way around? $\endgroup$