3
$\begingroup$

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

$\endgroup$
7
  • 2
    $\begingroup$ 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? $\endgroup$ Aug 22, 2017 at 16:42
  • $\begingroup$ 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}} $\endgroup$
    – Mudy Fa
    Aug 22, 2017 at 16:49
  • $\begingroup$ What should happen if the 'third list' were {{10,11},{12,13},{20,21}} rather than {{10,10},{10,10},{20,20}}? $\endgroup$
    – jjc385
    Aug 22, 2017 at 16:52
  • $\begingroup$ @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? $\endgroup$ Aug 22, 2017 at 16:53
  • $\begingroup$ @jjc385 this doesnt matter... its only importan to combine them in the right direction $\endgroup$
    – Mudy Fa
    Aug 22, 2017 at 16:54

4 Answers 4

3
$\begingroup$

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 *)
$\endgroup$
9
  • $\begingroup$ Dont work, theres one coma mistake $\endgroup$
    – Mudy Fa
    Aug 22, 2017 at 17:54
  • $\begingroup$ @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. $\endgroup$
    – Alan
    Aug 22, 2017 at 18:29
  • $\begingroup$ No i Mean, there is a mistake in Fold, just try it!! $\endgroup$
    – Mudy Fa
    Aug 23, 2017 at 12:48
  • $\begingroup$ @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. $\endgroup$
    – Alan
    Aug 23, 2017 at 13:23
  • $\begingroup$ oh yes, it works, but there is something between, } and ] in the End of the Fold line something like this "^" $\endgroup$
    – Mudy Fa
    Aug 23, 2017 at 13:38
2
$\begingroup$
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}}}

$\endgroup$
1
$\begingroup$

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

$\endgroup$
1
$\begingroup$
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}}}*)
$\endgroup$

Your Answer

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

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