7
$\begingroup$

I have data that, when imported to my notebook, is shown in the form:

list = {{1,2,3},{4,5,6},{7,8,9},{10,11,12}}

I want to transform this into:

list = {{{1,2},3},{{4,5},6},{{7,8},9},{{10,11},12}}

How can I do this?

$\endgroup$
1
  • $\begingroup$ My intent is make the data in a form that permite me do a multi-dimensional interpolation. $\endgroup$ May 26 at 15:42

11 Answers 11

7
$\begingroup$

Method 1 - No Map

With no Maps.

list = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};
Transpose@{list[[;; , ;; 2]], list[[;; , 3]]}

{{{1, 2}, 3}, {{4, 5}, 6}, {{7, 8}, 9}, {{10, 11}, 12}}

Method 2 - Apply

Not technically Map, but still iterating.

{{#1, #2}, #3} & @@@ list

{{{1, 2}, 3}, {{4, 5}, 6}, {{7, 8}, 9}, {{10, 11}, 12}}

$\endgroup$
9
$\begingroup$

Another way

list /. {x_, y_, z_} -> {{x, y}, z}

{{{1, 2}, 3}, {{4, 5}, 6}, {{7, 8}, 9}, {{10, 11}, 12}}

$\endgroup$
7
$\begingroup$

also you can do

lst = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}}
{#[[;; -2]], #[[-1]]} & /@ lst

Mathematica graphics

Also possible (if you like to view things as matrices)

a = Map[List, lst[[All, {1, -2}]]]
b = lst[[All, -1]]
MapThread[Append, {a, b}]

Mathematica graphics

8 more different 6 more ways could be possible.

$\endgroup$
6
$\begingroup$

You can do Map[{Most[#], Last[#]} &, list] to produce the desired output.

$\endgroup$
6
$\begingroup$
Through[{Most, Last}[#]] & /@ list
$\endgroup$
6
$\begingroup$

Using SequenceCases:

SequenceCases[list, {x_}:>{Most@x, Last@x}]
$\endgroup$
5
$\begingroup$

A rule-based approach:

list = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};
list /. {x__?NumericQ, y_?NumericQ} :> {{x}, y}

A simple Table-based approach:

Table[{list[[kk, 1 ;; 2]], list[[kk, 3]]}, {kk, 1, Length@list}]
$\endgroup$
5
$\begingroup$
BlockMap[Join,Riffle[list[[All,;;2]],list[[All,3]]],{2}]

(* {{{1, 2}, 3}, {{4, 5}, 6}, {{7, 8}, 9}, {{10, 11}, 12}} *)

In addition

Fold[List,#]&/@list

(* {{{1, 2}, 3}, {{4, 5}, 6}, {{7, 8}, 9}, {{10, 11}, 12}} *)
$\endgroup$
4
$\begingroup$

Drop and Append are sometimes useful. The following works:

Map[Append[{Drop[#, -1]}, Last[#]] &, list]
$\endgroup$
1
  • $\begingroup$ Instead of using Drop, use Most. It is clearer and shorter $\endgroup$ May 28 at 8:41
4
$\begingroup$

You can use Partition. All of the following work:

Map[{Flatten[Partition[#, 2]], #[[-1]]} &, list]

Map[{Flatten[Partition[#, 2]], #[[3]]} &, list]

Map[{First[Partition[#, 2]], #[[-1]]} &, list]

Map[{First[Partition[#, 2]], #[[3]]} &, list]

Map[{First[Partition[#, 2]], Last[#]} &, list]
$\endgroup$
4
$\begingroup$

The well known IA told me, with the exact question as prompt :

In[1]:= list = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}}
Out[1]= {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}}

In[2]:= transformedList = Map[{Take[#, 2], Last[#]} &, list]
Out[2]= {{{1, 2}, 3}, {{4, 5}, 6}, {{7, 8}, 9}, {{10, 11}, 12}}

And it works. I never touched mathics before. It's not (so much) ironic but to remember that new fact.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.