5
$\begingroup$

Suppose we have a list containing four sub-lists like that

data = {
 {{1, -1}, {2, 5}, {3, 9}, {4, 2}}, 
 {{1, -4}, {2, 0}, {3, 11}, {4, -1}}, 
 {{1, 3}, {2, 2}, {3, 22}, {4, 1}}, 
 {{1, 0}, {2, 0}, {3, -5}, {4, 7}}
 };

Now I want to create a new list, data2, containing the following

d1 = data[[1]];
d2 = data[[2]];
d3 = data[[3]];
d4 = data[[4]];
d11 = d1[[All, 1]];
d22 = d1[[All, 2]];
d33 = d2[[All, 2]];
d44 = d3[[All, 2]];
d55 = d4[[All, 2]];
data2 = Table[{d11[[i]], d22[[i]], d33[[i]], d44[[i]], d55[[i]]}, {i, 1, Length[d11]}]

which gives

{{1, -1, -4, 3, 0}, {2, 5, 0, 2, 0}, {3, 9, 11, 22, -5}, {4, 2, -1, 1, 7}}

I am pretty sure that there must be a quicker and much more elegant way of obtaining the new list.

Any suggestions?

Many thanks in advance!

$\endgroup$
9
$\begingroup$

In:

m = Map[Last, data, {2}];
v = First /@ First@data;
Thread[Prepend[m, v]]

Out:

{{1, -1, -4, 3, 0}, {2, 5, 0, 2, 0}, {3, 9, 11, 22, -5}, {4, 2, -1, 1,
   7}}
$\endgroup$
  • $\begingroup$ Very nice and quick! $\endgroup$ – Vaggelis_Z May 6 '17 at 15:52
8
$\begingroup$
Transpose[Prepend[data[[All, All, 2]], data[[1, All, 1]]]]

or

Flatten[{{data[[1, All, 1]]}, data[[All, All, 2]]}, {{3}, {1, 2}}]

or

Drop[Flatten[data, {{2}, {1, 3}}], {}, {3, -2, 2}]
$\endgroup$
  • $\begingroup$ Nice! Let's wait for a while to see if someone else has anything else to suggest. $\endgroup$ – Vaggelis_Z May 6 '17 at 15:19
  • 2
    $\begingroup$ In your last one you could flatten levels 1 and 3 the other way and so put all the dropped elements together Flatten[data, {{2}, {3, 1}}][[All, 4 ;;]] $\endgroup$ – Simon Woods May 6 '17 at 22:36
6
$\begingroup$

here you are

l1 = Length@data;
l2 = First[Length /@ data];
Table[d = Table[Join[data[[i]][[j]]], {i, 1, l1}];
Drop[Join[First /@ d, Last /@ d], l1 - 1], {j, 1, l2}]

{{1, -1, -4, 3, 0}, {2, 5, 0, 2, 0}, {3, 9, 11, 22, -5}, {4, 2, -1, 1, 7}}

$\endgroup$
  • $\begingroup$ It works only when four sub-lists are present. I tested it in a case with 1000 lists and it failed. $\endgroup$ – Vaggelis_Z May 6 '17 at 15:27
  • $\begingroup$ I think you are wrong. I never used the number 4. I used length@data $\endgroup$ – J42161217 May 6 '17 at 15:30
  • $\begingroup$ Take for example the list data = { {{1, -1}, {2, 5}, {3, 9}, {4, 2}, {5, 1}}, {{1, -4}, {2, 0}, {3, 11}, {4, -1}, {5, 1}}, {{1, 3}, {2, 2}, {3, 22}, {4, 1}, {5, 1}}, {{1, 0}, {2, 0}, {3, -5}, {4, 7}, {5, 1}} }; The method proposed buy Coolwater gives the correct answer, while yours is not correct. $\endgroup$ – Vaggelis_Z May 6 '17 at 15:40
  • $\begingroup$ I fixed it... it was working for nxn $\endgroup$ – J42161217 May 6 '17 at 15:43
  • 2
    $\begingroup$ Now it works fine! $\endgroup$ – Vaggelis_Z May 6 '17 at 15:45
6
$\begingroup$
Join[{#[[1]]}, #2] & @@@ Transpose[data, {3, 1, 2}]

{{1, -1, -4, 3, 0}, {2, 5, 0, 2, 0}, {3, 9, 11, 22, -5}, {4, 2, -1, 1, 7}}

$\endgroup$
5
$\begingroup$
Join[#[[1]], #[[2 ;;, 2]]] & /@ (Transpose@data)

{{1, -1, -4, 3, 0}, {2, 5, 0, 2, 0}, {3, 9, 11, 22, -5}, {4, 2, -1, 1, 7}}

$\endgroup$
4
$\begingroup$

I found this line of thought most intuitive:

Transpose[Join[data[[{1}, ;; , 1]], data[[;; , ;; , 2]]]]

If you would call all your pairs {x,y} then this takes all y`s and prepends a single x. This is already the transposed version of what you want.

It is very similar to the first option provided by coolwater, but perhaps more people intuitively understand Join than Prepend.

$\endgroup$
3
$\begingroup$
Drop[#, {3, 8, 2}] & /@ Partition[Flatten[Transpose@data], 8]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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