Message Boards Message Boards

GROUPS:

[✓] Concatenate elements in a list?

Posted 1 year ago
1230 Views
|
3 Replies
|
4 Total Likes
|

I'm very new to Mathematica, so here comes a newbie question.

I have a list of numbers, such as {12,35}, and want to concatenate them to produce a single-element list: {1235} in this example. I then want to extract that element as an integer, 1235. Can someone please give me a clue.

Geoffrey Marnell

3 Replies

You can turn a number into a list of its digits using IntegerDigits. If you do that on all your numbers, you have a list where each element is a list of digits. Then you can Flatten this list to get one long list of digits. After that, you rebuild a new Integer.

FromDigits[Flatten[IntegerDigits[{12, 35}]]]

(* 1235 *)

Beat me to it!

IntegerDigits will break up the numbers, into sub-lists

IntegerDigits@{12, 35}

{{1, 2}, {3, 5}}

Flatten will break up the sub-lists

Flatten@IntegerDigits@{12, 35}

{1, 2, 3, 5}

FromDigits will make a new integer from the flattened list

FromDigits@Flatten@IntegerDigits@{12, 35}

1235

RightComposition allows the operations to be read from left to right

{12, 35} // RightComposition[IntegerDigits, Flatten, FromDigits]

1235

Another way of saying RightComposition is /*

RightComposition[
 IntegerDigits,
 Flatten,
 FromDigits]

IntegerDigits /* Flatten /* FromDigits

Make a compound operation called joinDigits, or as you like

joinDigits = IntegerDigits /* Flatten /* FromDigits;

Call the new operation on your list

joinDigits[{12, 35}]

1235

joinDigits[{12, 35, 69}]

123569

Thanks Christopher, thanks Patrick. Geoffrey Marnell

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract