3

I have a question about combining lists, I really tried it, but that's so hard for me. I'm a newbie.

I have this kind of a list:

a={{{2,3},{1,2},{3,5}},{{1,9},{1,5},{1,7}},{{10,10},{10,10},{20,20}}}

I need this kind of result:

res={{{2,3},{1,2},{3,5},{10,10},{20}},{{1,9},{1,5},{1,7},{10,10},{20}}}

My list can have 1000 of elements...

I would be thankful for your help

user64494
  • 26,149
  • 4
  • 27
  • 56
Mudy Fa
  • 197
  • 5
  • 2
    Can you clarify what you want to achieve? For example, which of the input's 10s end up where in the desired output? Why should the {10,10},{20} in the output (twice) be grouped like that? – Jules Lamers Aug 22 '17 at 16:42
  • These are 3 lists, which i already combine:

    a1= {{2,3},{1,2},{3,5}},{{1,9},{1,5},{1,7}} a2={{10,10},{10,10}} a3={{20,20}}

    – Mudy Fa Aug 22 '17 at 16:49
  • What should happen if the 'third list' were {{10,11},{12,13},{20,21}} rather than {{10,10},{10,10},{20,20}}? – jjc385 Aug 22 '17 at 16:52
  • @MudyFa But your a1 is not a list (missing {}s?), and your question does not involve a2 and a3 but rather a2~Join~a3... Could you be a bit more precise? – Jules Lamers Aug 22 '17 at 16:53
  • @jjc385 this doesnt matter... its only importan to combine them in the right direction – Mudy Fa Aug 22 '17 at 16:54
  • yes im sorry, u have to put on more {} belong all of them! – Mudy Fa Aug 22 '17 at 16:55
  • I'm asking you to clarify what the output should be if the 'third list' were {{10,11},{12,13},{20,21}} rather than {{10,10},{10,10},{20,20}}. As you've described it, it's unclear what should happen. If you're not interested in that case, then how about {{10,10},{15,15},{20,20}}? It's just not clear enough from the question (at least to me) what should happen in these cases. – jjc385 Aug 22 '17 at 16:59

4 Answers4

3

If I assume some typos in your presentation, this seems to be another question about how to append columns.

a1 = {{{2, 3}, {1, 2}, {3, 5}}, {{1, 9}, {1, 5}, {1, 7}}};
a2 = {{10, 10}, {10, 10}};
a3 = {{20}, {20}};
Fold[MapThread[Append, {#1, #2}] &, {a1, a2, a3}] (* appends all*)
% // MatrixForm  (* display nicely *)
Alan
  • 13,686
  • 19
  • 38
  • Dont work, theres one coma mistake – Mudy Fa Aug 22 '17 at 17:54
  • @MudyFa Please copy the entire code. It runs fine and produces your desired output. Note that my a3 is a correction of what you wrote: you said there were missing braces, so I added the braces needed for the format to make sense. – Alan Aug 22 '17 at 18:29
  • No i Mean, there is a mistake in Fold, just try it!! – Mudy Fa Aug 23 '17 at 12:48
  • @MudyFa You must have copied it wrong. It works as it should. If you are copying and pasting, that will sometimes lose end-of-lines. But the code is good. – Alan Aug 23 '17 at 13:23
  • oh yes, it works, but there is something between, } and ] in the End of the Fold line something like this "^" – Mudy Fa Aug 23 '17 at 13:38
  • @MudyFa Odd, I see nothing of the sort, and I certainly pasted nothing of the sort. Do you actually see this in your browser? What browser are you using? – Alan Aug 23 '17 at 15:20
  • version 9.0... hm i dont know – Mudy Fa Aug 23 '17 at 15:42
  • but what is , when i have a={{1,2},{2,1}} and b ={{1,3},{4,1}}

    my result with MapThread[Append[#1,#2]&,{a,b}] is :

    {{1,2,{1,3}},{2,1,{4,1}}} ?? i need :

    {{{1,2},{1,3}},{{2,1},{4,1}}} ... i dont get this... the position is right but but the clamp not... can u help again please?

    – Mudy Fa Aug 23 '17 at 16:42
  • @MudyFa That is a very different question, because now a and b are matrices whereas your a1 was a list of matrices. I would write your new problem as Transpose[{a, b}]. – Alan Aug 23 '17 at 23:22
2
a = {{{2, 3}, {1, 2}, {3, 5}}, {{1, 9}, {1, 5}, {1, 7}}};
b = {{10, 10}, {10, 10}};
c = {{20}, {20}};

Another possibility

MapThread[Join[#1, {#2}, {#3}] &, {a, b, c}] 

{{{2, 3}, {1, 2}, {3, 5}, {10, 10}, {20}}, {{1, 9}, {1, 5}, {1, 7}, {10, 10}, {20}}}

eldo
  • 67,911
  • 5
  • 60
  • 168
1

I have changed a few entries to visually differentiate where the items are landing after various operations. Matrix dimensions, however, remain the same as those from the accepted answer.

a = {{{2, 3}, {1, 2}, {3, 5}}, {{1, 9}, {1, 5}, {1, 7}}};
b = {{10, 20}, {30, 40}};
c = {{25}, {35}};

g1 = MapThread[Join@## &, {a, List /@ b, List /@ c}];

g2 = MapThread[Join, {a, List /@ b, List /@ c}];

g3 = MapThread[Catenate@{##} &, {a, List /@ b, List /@ c}];

g1 == g2 == g3

(* True *)


Result:

MatrixForm /@ {a, b, c, g1}

enter image description here

Syed
  • 52,495
  • 4
  • 30
  • 85
1
a = {{{2, 3}, {1, 2}, {3, 5}}, {{1, 9}, {1, 5}, {1, 7}}};
b = {{10, 10}, {10, 10}};
c = {{20}, {20}};

Using Thread and ReplaceAll:

ReplaceAll[m_?MatrixQ :> Splice@m][Thread[{a, b, c}]]

({{{2, 3}, {1, 2}, {3, 5}, {10, 10}, {20}}, {{1, 9}, {1, 5}, {1, 7}, {10, 10}, {20}}})

E. Chan-López
  • 23,117
  • 3
  • 21
  • 44