How to convert a list of digits to a string in Mathematica
Clash Royale CLAN TAG#URR8PPP
up vote
4
down vote
favorite
I want to convert a list of digits to a string, for example from 1,2,3,4,5
, I want to get the string 12345.
The following will work as long as the list does not begin with 0's:
1, 2, 3, 4, 5 // FromDigits // ToString
Out[10]:= 12345
However if the list begins with 0's, as in
0, 0, 1, 2, 3, 4 // FromDigits // ToString
Out[11]:= 1234
this method chops off the 0's in the front.
I could write a program that tests to see if the list begins with a string of 0's and then append that many zeros to the chopped off output, but I'd like a less procedural way of getting the string. Is this possible?
Note: the lists I want to convert are going to come from some prior computation, so unlike the above examples, I won't know their contents unless I explicitely examine them.
list-manipulation conversion
add a comment |Â
up vote
4
down vote
favorite
I want to convert a list of digits to a string, for example from 1,2,3,4,5
, I want to get the string 12345.
The following will work as long as the list does not begin with 0's:
1, 2, 3, 4, 5 // FromDigits // ToString
Out[10]:= 12345
However if the list begins with 0's, as in
0, 0, 1, 2, 3, 4 // FromDigits // ToString
Out[11]:= 1234
this method chops off the 0's in the front.
I could write a program that tests to see if the list begins with a string of 0's and then append that many zeros to the chopped off output, but I'd like a less procedural way of getting the string. Is this possible?
Note: the lists I want to convert are going to come from some prior computation, so unlike the above examples, I won't know their contents unless I explicitely examine them.
list-manipulation conversion
TryStringRiffle[0, 0, 1, 2, 3, 4, ""]
introduced in version 10.1
– Hans
Sep 2 at 1:47
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I want to convert a list of digits to a string, for example from 1,2,3,4,5
, I want to get the string 12345.
The following will work as long as the list does not begin with 0's:
1, 2, 3, 4, 5 // FromDigits // ToString
Out[10]:= 12345
However if the list begins with 0's, as in
0, 0, 1, 2, 3, 4 // FromDigits // ToString
Out[11]:= 1234
this method chops off the 0's in the front.
I could write a program that tests to see if the list begins with a string of 0's and then append that many zeros to the chopped off output, but I'd like a less procedural way of getting the string. Is this possible?
Note: the lists I want to convert are going to come from some prior computation, so unlike the above examples, I won't know their contents unless I explicitely examine them.
list-manipulation conversion
I want to convert a list of digits to a string, for example from 1,2,3,4,5
, I want to get the string 12345.
The following will work as long as the list does not begin with 0's:
1, 2, 3, 4, 5 // FromDigits // ToString
Out[10]:= 12345
However if the list begins with 0's, as in
0, 0, 1, 2, 3, 4 // FromDigits // ToString
Out[11]:= 1234
this method chops off the 0's in the front.
I could write a program that tests to see if the list begins with a string of 0's and then append that many zeros to the chopped off output, but I'd like a less procedural way of getting the string. Is this possible?
Note: the lists I want to convert are going to come from some prior computation, so unlike the above examples, I won't know their contents unless I explicitely examine them.
list-manipulation conversion
edited Sep 2 at 1:33


m0nhawk
2,47811431
2,47811431
asked Sep 2 at 0:04
hagbard7000
262
262
TryStringRiffle[0, 0, 1, 2, 3, 4, ""]
introduced in version 10.1
– Hans
Sep 2 at 1:47
add a comment |Â
TryStringRiffle[0, 0, 1, 2, 3, 4, ""]
introduced in version 10.1
– Hans
Sep 2 at 1:47
Try
StringRiffle[0, 0, 1, 2, 3, 4, ""]
introduced in version 10.1– Hans
Sep 2 at 1:47
Try
StringRiffle[0, 0, 1, 2, 3, 4, ""]
introduced in version 10.1– Hans
Sep 2 at 1:47
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
9
down vote
Try
StringRiffle[0, 0, 1, 2, 3, 4, ""]
"001234"
Introduced in version 10.1 or in postfix as follows:
0, 0, 1, 2, 3, 4 // StringRiffle[#, ""] &
add a comment |Â
up vote
7
down vote
While StringRiffle
is nice and concise, but it has -- similar to many of the String
-related tools in Mathematica -- severe performance issues. For example, the combination of IntegerString
and StringJoin
is ten times faster. Even roman465's somewhat creative solution to first convert the whole expression to a string and to remove the undesired parts afterwards is five times faster. If you can be be sure that only digits from 0
to 9
appear in the list, you can also use FromCharacterCode
which is faster by almost two further orders of magnitude.
a = RandomInteger[0, 9, 100000];
s1 = StringRiffle[a, ""]; // RepeatedTiming // First
s2 = a // ToString // StringDelete[#, "", ", ", ""] &; // RepeatedTiming // First
s3 = StringJoin[IntegerString[a]]; // RepeatedTiming // First
s4 = FromCharacterCode[a + 48]; // RepeatedTiming // First
s1 == s2 == s3 == s4
0.311
0.0649
0.036
0.000534
True
add a comment |Â
up vote
4
down vote
It might be more convenient to convert your list to a string first, then remove unnecessary symbols.
0, 0, 1, 2, 3, 4 // ToString // StringDelete[#, "", ", ", ""] &
"001234"
add a comment |Â
up vote
4
down vote
It can also be done in this way
StringJoin[ToString /@ 0, 0, 1, 2, 3, 4]
This feels like the most readable and syntactically correct to me.
– Emilio Pisanty
Sep 2 at 10:46
@EmilioPisanty Thx.
– Î‘λÎÂξανδÃÂο Ζεγγ
Sep 2 at 10:56
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
9
down vote
Try
StringRiffle[0, 0, 1, 2, 3, 4, ""]
"001234"
Introduced in version 10.1 or in postfix as follows:
0, 0, 1, 2, 3, 4 // StringRiffle[#, ""] &
add a comment |Â
up vote
9
down vote
Try
StringRiffle[0, 0, 1, 2, 3, 4, ""]
"001234"
Introduced in version 10.1 or in postfix as follows:
0, 0, 1, 2, 3, 4 // StringRiffle[#, ""] &
add a comment |Â
up vote
9
down vote
up vote
9
down vote
Try
StringRiffle[0, 0, 1, 2, 3, 4, ""]
"001234"
Introduced in version 10.1 or in postfix as follows:
0, 0, 1, 2, 3, 4 // StringRiffle[#, ""] &
Try
StringRiffle[0, 0, 1, 2, 3, 4, ""]
"001234"
Introduced in version 10.1 or in postfix as follows:
0, 0, 1, 2, 3, 4 // StringRiffle[#, ""] &
answered Sep 2 at 2:01
Hans
76146
76146
add a comment |Â
add a comment |Â
up vote
7
down vote
While StringRiffle
is nice and concise, but it has -- similar to many of the String
-related tools in Mathematica -- severe performance issues. For example, the combination of IntegerString
and StringJoin
is ten times faster. Even roman465's somewhat creative solution to first convert the whole expression to a string and to remove the undesired parts afterwards is five times faster. If you can be be sure that only digits from 0
to 9
appear in the list, you can also use FromCharacterCode
which is faster by almost two further orders of magnitude.
a = RandomInteger[0, 9, 100000];
s1 = StringRiffle[a, ""]; // RepeatedTiming // First
s2 = a // ToString // StringDelete[#, "", ", ", ""] &; // RepeatedTiming // First
s3 = StringJoin[IntegerString[a]]; // RepeatedTiming // First
s4 = FromCharacterCode[a + 48]; // RepeatedTiming // First
s1 == s2 == s3 == s4
0.311
0.0649
0.036
0.000534
True
add a comment |Â
up vote
7
down vote
While StringRiffle
is nice and concise, but it has -- similar to many of the String
-related tools in Mathematica -- severe performance issues. For example, the combination of IntegerString
and StringJoin
is ten times faster. Even roman465's somewhat creative solution to first convert the whole expression to a string and to remove the undesired parts afterwards is five times faster. If you can be be sure that only digits from 0
to 9
appear in the list, you can also use FromCharacterCode
which is faster by almost two further orders of magnitude.
a = RandomInteger[0, 9, 100000];
s1 = StringRiffle[a, ""]; // RepeatedTiming // First
s2 = a // ToString // StringDelete[#, "", ", ", ""] &; // RepeatedTiming // First
s3 = StringJoin[IntegerString[a]]; // RepeatedTiming // First
s4 = FromCharacterCode[a + 48]; // RepeatedTiming // First
s1 == s2 == s3 == s4
0.311
0.0649
0.036
0.000534
True
add a comment |Â
up vote
7
down vote
up vote
7
down vote
While StringRiffle
is nice and concise, but it has -- similar to many of the String
-related tools in Mathematica -- severe performance issues. For example, the combination of IntegerString
and StringJoin
is ten times faster. Even roman465's somewhat creative solution to first convert the whole expression to a string and to remove the undesired parts afterwards is five times faster. If you can be be sure that only digits from 0
to 9
appear in the list, you can also use FromCharacterCode
which is faster by almost two further orders of magnitude.
a = RandomInteger[0, 9, 100000];
s1 = StringRiffle[a, ""]; // RepeatedTiming // First
s2 = a // ToString // StringDelete[#, "", ", ", ""] &; // RepeatedTiming // First
s3 = StringJoin[IntegerString[a]]; // RepeatedTiming // First
s4 = FromCharacterCode[a + 48]; // RepeatedTiming // First
s1 == s2 == s3 == s4
0.311
0.0649
0.036
0.000534
True
While StringRiffle
is nice and concise, but it has -- similar to many of the String
-related tools in Mathematica -- severe performance issues. For example, the combination of IntegerString
and StringJoin
is ten times faster. Even roman465's somewhat creative solution to first convert the whole expression to a string and to remove the undesired parts afterwards is five times faster. If you can be be sure that only digits from 0
to 9
appear in the list, you can also use FromCharacterCode
which is faster by almost two further orders of magnitude.
a = RandomInteger[0, 9, 100000];
s1 = StringRiffle[a, ""]; // RepeatedTiming // First
s2 = a // ToString // StringDelete[#, "", ", ", ""] &; // RepeatedTiming // First
s3 = StringJoin[IntegerString[a]]; // RepeatedTiming // First
s4 = FromCharacterCode[a + 48]; // RepeatedTiming // First
s1 == s2 == s3 == s4
0.311
0.0649
0.036
0.000534
True
edited Sep 2 at 22:08
answered Sep 2 at 9:31


Henrik Schumacher
36.9k249105
36.9k249105
add a comment |Â
add a comment |Â
up vote
4
down vote
It might be more convenient to convert your list to a string first, then remove unnecessary symbols.
0, 0, 1, 2, 3, 4 // ToString // StringDelete[#, "", ", ", ""] &
"001234"
add a comment |Â
up vote
4
down vote
It might be more convenient to convert your list to a string first, then remove unnecessary symbols.
0, 0, 1, 2, 3, 4 // ToString // StringDelete[#, "", ", ", ""] &
"001234"
add a comment |Â
up vote
4
down vote
up vote
4
down vote
It might be more convenient to convert your list to a string first, then remove unnecessary symbols.
0, 0, 1, 2, 3, 4 // ToString // StringDelete[#, "", ", ", ""] &
"001234"
It might be more convenient to convert your list to a string first, then remove unnecessary symbols.
0, 0, 1, 2, 3, 4 // ToString // StringDelete[#, "", ", ", ""] &
"001234"
edited Sep 2 at 9:31


Henrik Schumacher
36.9k249105
36.9k249105
answered Sep 2 at 0:25
roman465
739313
739313
add a comment |Â
add a comment |Â
up vote
4
down vote
It can also be done in this way
StringJoin[ToString /@ 0, 0, 1, 2, 3, 4]
This feels like the most readable and syntactically correct to me.
– Emilio Pisanty
Sep 2 at 10:46
@EmilioPisanty Thx.
– Î‘λÎÂξανδÃÂο Ζεγγ
Sep 2 at 10:56
add a comment |Â
up vote
4
down vote
It can also be done in this way
StringJoin[ToString /@ 0, 0, 1, 2, 3, 4]
This feels like the most readable and syntactically correct to me.
– Emilio Pisanty
Sep 2 at 10:46
@EmilioPisanty Thx.
– Î‘λÎÂξανδÃÂο Ζεγγ
Sep 2 at 10:56
add a comment |Â
up vote
4
down vote
up vote
4
down vote
It can also be done in this way
StringJoin[ToString /@ 0, 0, 1, 2, 3, 4]
It can also be done in this way
StringJoin[ToString /@ 0, 0, 1, 2, 3, 4]
answered Sep 2 at 9:58


ΑλÎÂξανδÃÂο Ζεγγ
1,523720
1,523720
This feels like the most readable and syntactically correct to me.
– Emilio Pisanty
Sep 2 at 10:46
@EmilioPisanty Thx.
– Î‘λÎÂξανδÃÂο Ζεγγ
Sep 2 at 10:56
add a comment |Â
This feels like the most readable and syntactically correct to me.
– Emilio Pisanty
Sep 2 at 10:46
@EmilioPisanty Thx.
– Î‘λÎÂξανδÃÂο Ζεγγ
Sep 2 at 10:56
This feels like the most readable and syntactically correct to me.
– Emilio Pisanty
Sep 2 at 10:46
This feels like the most readable and syntactically correct to me.
– Emilio Pisanty
Sep 2 at 10:46
@EmilioPisanty Thx.
– Î‘λÎÂξανδÃÂο Ζεγγ
Sep 2 at 10:56
@EmilioPisanty Thx.
– Î‘λÎÂξανδÃÂο Ζεγγ
Sep 2 at 10:56
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181078%2fhow-to-convert-a-list-of-digits-to-a-string-in-mathematica%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Try
StringRiffle[0, 0, 1, 2, 3, 4, ""]
introduced in version 10.1– Hans
Sep 2 at 1:47