How to convert list of digits to number? [closed]
I have a list such as:
a = {1, 2, 4, 3};
and I want to get the 'number' of the list. For this example, I want to get this number: 1243
. I do not know how to get it easily. For now, my solution is:
a[[1]]*10^3+a[[2]]*10^2+a[[3]]*10^1+a[[4]];
It works. But it is too complex. I want to know a way more convenient.
functions special-functions
closed as off-topic by corey979, Daniel Lichtblau, AccidentalFourierTransform, Michael E2, eyorble Dec 8 at 17:04
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – corey979, Daniel Lichtblau, AccidentalFourierTransform, Michael E2, eyorble
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I have a list such as:
a = {1, 2, 4, 3};
and I want to get the 'number' of the list. For this example, I want to get this number: 1243
. I do not know how to get it easily. For now, my solution is:
a[[1]]*10^3+a[[2]]*10^2+a[[3]]*10^1+a[[4]];
It works. But it is too complex. I want to know a way more convenient.
functions special-functions
closed as off-topic by corey979, Daniel Lichtblau, AccidentalFourierTransform, Michael E2, eyorble Dec 8 at 17:04
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – corey979, Daniel Lichtblau, AccidentalFourierTransform, Michael E2, eyorble
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I have a list such as:
a = {1, 2, 4, 3};
and I want to get the 'number' of the list. For this example, I want to get this number: 1243
. I do not know how to get it easily. For now, my solution is:
a[[1]]*10^3+a[[2]]*10^2+a[[3]]*10^1+a[[4]];
It works. But it is too complex. I want to know a way more convenient.
functions special-functions
I have a list such as:
a = {1, 2, 4, 3};
and I want to get the 'number' of the list. For this example, I want to get this number: 1243
. I do not know how to get it easily. For now, my solution is:
a[[1]]*10^3+a[[2]]*10^2+a[[3]]*10^1+a[[4]];
It works. But it is too complex. I want to know a way more convenient.
functions special-functions
functions special-functions
edited Dec 8 at 9:46
Henrik Schumacher
48.6k467137
48.6k467137
asked Dec 8 at 5:04
user61054
334
334
closed as off-topic by corey979, Daniel Lichtblau, AccidentalFourierTransform, Michael E2, eyorble Dec 8 at 17:04
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – corey979, Daniel Lichtblau, AccidentalFourierTransform, Michael E2, eyorble
If this question can be reworded to fit the rules in the help center, please edit the question.
closed as off-topic by corey979, Daniel Lichtblau, AccidentalFourierTransform, Michael E2, eyorble Dec 8 at 17:04
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – corey979, Daniel Lichtblau, AccidentalFourierTransform, Michael E2, eyorble
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
a = {1, 2, 4, 3};
FromDigits[a]
(* 1243 *)
add a comment |
Other than the built-in FromDigits
, we can build our wheel
Fold[{10, 1}.{##} &, a]
I want to know what is the mean of 'wheel'. Is it a function to achieve something? I know what you express, but I am interest in the meaning of 'wheel'.
– user61054
Dec 8 at 6:40
@user61054 Do you want to know what the figurative meaning of "wheel" or how the functionFold
works?
– Αλέξανδρος Ζεγγ
Dec 8 at 11:28
add a comment |
Best way would be to use build-in function FromDigits
as shown above.
But just in case you'd like a convoluted way to do it, here is another option
a = {1, 2, 4, 3};
ToExpression[StringJoin[ToString[#] & /@ a]]
add a comment |
These are some methods I would not recommend:
lst = {1, 2, 3, 4};
f = (Curry[StringRiffle][""] /* ToExpression);
g = (Through[{
Identity,
Length /* Range /* Reverse /* Curry[Plus][-1] /* Curry[Power, 2][10]
}[#]] & /* MapThread[Times] /* Total);
f[lst] (* 1234 *)
g[lst] (* 1234 *)
Are you really serious that you would not recommend this? :)
– Buddha_the_Scientist
Dec 8 at 12:15
add a comment |
Here is a method that is faster than FromDigits
for the construction of many numbers at once:
a = RandomInteger[{1, 9}, {1000000, 10}];
r1 = FromDigits /@ a; // RepeatedTiming // First
r2 = FromDigits[Transpose[a]]; // RepeatedTiming
r3 = a.(10^Range[9, 0, -1]); // RepeatedTiming // First
r1 == r2 == r3
0.466
0.068
0.040
True
Admittedly, FromDigits[Transpose[a]]
is only slower than a.(10^Range[9, 0, -1])
because of Transpose
.
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
a = {1, 2, 4, 3};
FromDigits[a]
(* 1243 *)
add a comment |
a = {1, 2, 4, 3};
FromDigits[a]
(* 1243 *)
add a comment |
a = {1, 2, 4, 3};
FromDigits[a]
(* 1243 *)
a = {1, 2, 4, 3};
FromDigits[a]
(* 1243 *)
edited Dec 8 at 11:49
Αλέξανδρος Ζεγγ
4,0441928
4,0441928
answered Dec 8 at 5:10
Mike Honeychurch
30.9k268143
30.9k268143
add a comment |
add a comment |
Other than the built-in FromDigits
, we can build our wheel
Fold[{10, 1}.{##} &, a]
I want to know what is the mean of 'wheel'. Is it a function to achieve something? I know what you express, but I am interest in the meaning of 'wheel'.
– user61054
Dec 8 at 6:40
@user61054 Do you want to know what the figurative meaning of "wheel" or how the functionFold
works?
– Αλέξανδρος Ζεγγ
Dec 8 at 11:28
add a comment |
Other than the built-in FromDigits
, we can build our wheel
Fold[{10, 1}.{##} &, a]
I want to know what is the mean of 'wheel'. Is it a function to achieve something? I know what you express, but I am interest in the meaning of 'wheel'.
– user61054
Dec 8 at 6:40
@user61054 Do you want to know what the figurative meaning of "wheel" or how the functionFold
works?
– Αλέξανδρος Ζεγγ
Dec 8 at 11:28
add a comment |
Other than the built-in FromDigits
, we can build our wheel
Fold[{10, 1}.{##} &, a]
Other than the built-in FromDigits
, we can build our wheel
Fold[{10, 1}.{##} &, a]
answered Dec 8 at 5:28
Αλέξανδρος Ζεγγ
4,0441928
4,0441928
I want to know what is the mean of 'wheel'. Is it a function to achieve something? I know what you express, but I am interest in the meaning of 'wheel'.
– user61054
Dec 8 at 6:40
@user61054 Do you want to know what the figurative meaning of "wheel" or how the functionFold
works?
– Αλέξανδρος Ζεγγ
Dec 8 at 11:28
add a comment |
I want to know what is the mean of 'wheel'. Is it a function to achieve something? I know what you express, but I am interest in the meaning of 'wheel'.
– user61054
Dec 8 at 6:40
@user61054 Do you want to know what the figurative meaning of "wheel" or how the functionFold
works?
– Αλέξανδρος Ζεγγ
Dec 8 at 11:28
I want to know what is the mean of 'wheel'. Is it a function to achieve something? I know what you express, but I am interest in the meaning of 'wheel'.
– user61054
Dec 8 at 6:40
I want to know what is the mean of 'wheel'. Is it a function to achieve something? I know what you express, but I am interest in the meaning of 'wheel'.
– user61054
Dec 8 at 6:40
@user61054 Do you want to know what the figurative meaning of "wheel" or how the function
Fold
works?– Αλέξανδρος Ζεγγ
Dec 8 at 11:28
@user61054 Do you want to know what the figurative meaning of "wheel" or how the function
Fold
works?– Αλέξανδρος Ζεγγ
Dec 8 at 11:28
add a comment |
Best way would be to use build-in function FromDigits
as shown above.
But just in case you'd like a convoluted way to do it, here is another option
a = {1, 2, 4, 3};
ToExpression[StringJoin[ToString[#] & /@ a]]
add a comment |
Best way would be to use build-in function FromDigits
as shown above.
But just in case you'd like a convoluted way to do it, here is another option
a = {1, 2, 4, 3};
ToExpression[StringJoin[ToString[#] & /@ a]]
add a comment |
Best way would be to use build-in function FromDigits
as shown above.
But just in case you'd like a convoluted way to do it, here is another option
a = {1, 2, 4, 3};
ToExpression[StringJoin[ToString[#] & /@ a]]
Best way would be to use build-in function FromDigits
as shown above.
But just in case you'd like a convoluted way to do it, here is another option
a = {1, 2, 4, 3};
ToExpression[StringJoin[ToString[#] & /@ a]]
answered Dec 8 at 6:18
Nasser
57.2k486205
57.2k486205
add a comment |
add a comment |
These are some methods I would not recommend:
lst = {1, 2, 3, 4};
f = (Curry[StringRiffle][""] /* ToExpression);
g = (Through[{
Identity,
Length /* Range /* Reverse /* Curry[Plus][-1] /* Curry[Power, 2][10]
}[#]] & /* MapThread[Times] /* Total);
f[lst] (* 1234 *)
g[lst] (* 1234 *)
Are you really serious that you would not recommend this? :)
– Buddha_the_Scientist
Dec 8 at 12:15
add a comment |
These are some methods I would not recommend:
lst = {1, 2, 3, 4};
f = (Curry[StringRiffle][""] /* ToExpression);
g = (Through[{
Identity,
Length /* Range /* Reverse /* Curry[Plus][-1] /* Curry[Power, 2][10]
}[#]] & /* MapThread[Times] /* Total);
f[lst] (* 1234 *)
g[lst] (* 1234 *)
Are you really serious that you would not recommend this? :)
– Buddha_the_Scientist
Dec 8 at 12:15
add a comment |
These are some methods I would not recommend:
lst = {1, 2, 3, 4};
f = (Curry[StringRiffle][""] /* ToExpression);
g = (Through[{
Identity,
Length /* Range /* Reverse /* Curry[Plus][-1] /* Curry[Power, 2][10]
}[#]] & /* MapThread[Times] /* Total);
f[lst] (* 1234 *)
g[lst] (* 1234 *)
These are some methods I would not recommend:
lst = {1, 2, 3, 4};
f = (Curry[StringRiffle][""] /* ToExpression);
g = (Through[{
Identity,
Length /* Range /* Reverse /* Curry[Plus][-1] /* Curry[Power, 2][10]
}[#]] & /* MapThread[Times] /* Total);
f[lst] (* 1234 *)
g[lst] (* 1234 *)
answered Dec 8 at 6:30
Shredderroy
1,4931115
1,4931115
Are you really serious that you would not recommend this? :)
– Buddha_the_Scientist
Dec 8 at 12:15
add a comment |
Are you really serious that you would not recommend this? :)
– Buddha_the_Scientist
Dec 8 at 12:15
Are you really serious that you would not recommend this? :)
– Buddha_the_Scientist
Dec 8 at 12:15
Are you really serious that you would not recommend this? :)
– Buddha_the_Scientist
Dec 8 at 12:15
add a comment |
Here is a method that is faster than FromDigits
for the construction of many numbers at once:
a = RandomInteger[{1, 9}, {1000000, 10}];
r1 = FromDigits /@ a; // RepeatedTiming // First
r2 = FromDigits[Transpose[a]]; // RepeatedTiming
r3 = a.(10^Range[9, 0, -1]); // RepeatedTiming // First
r1 == r2 == r3
0.466
0.068
0.040
True
Admittedly, FromDigits[Transpose[a]]
is only slower than a.(10^Range[9, 0, -1])
because of Transpose
.
add a comment |
Here is a method that is faster than FromDigits
for the construction of many numbers at once:
a = RandomInteger[{1, 9}, {1000000, 10}];
r1 = FromDigits /@ a; // RepeatedTiming // First
r2 = FromDigits[Transpose[a]]; // RepeatedTiming
r3 = a.(10^Range[9, 0, -1]); // RepeatedTiming // First
r1 == r2 == r3
0.466
0.068
0.040
True
Admittedly, FromDigits[Transpose[a]]
is only slower than a.(10^Range[9, 0, -1])
because of Transpose
.
add a comment |
Here is a method that is faster than FromDigits
for the construction of many numbers at once:
a = RandomInteger[{1, 9}, {1000000, 10}];
r1 = FromDigits /@ a; // RepeatedTiming // First
r2 = FromDigits[Transpose[a]]; // RepeatedTiming
r3 = a.(10^Range[9, 0, -1]); // RepeatedTiming // First
r1 == r2 == r3
0.466
0.068
0.040
True
Admittedly, FromDigits[Transpose[a]]
is only slower than a.(10^Range[9, 0, -1])
because of Transpose
.
Here is a method that is faster than FromDigits
for the construction of many numbers at once:
a = RandomInteger[{1, 9}, {1000000, 10}];
r1 = FromDigits /@ a; // RepeatedTiming // First
r2 = FromDigits[Transpose[a]]; // RepeatedTiming
r3 = a.(10^Range[9, 0, -1]); // RepeatedTiming // First
r1 == r2 == r3
0.466
0.068
0.040
True
Admittedly, FromDigits[Transpose[a]]
is only slower than a.(10^Range[9, 0, -1])
because of Transpose
.
edited Dec 8 at 9:52
corey979
20.7k64282
20.7k64282
answered Dec 8 at 6:57
Henrik Schumacher
48.6k467137
48.6k467137
add a comment |
add a comment |