Search code examples
wolfram-mathematicanested-lists

Rearranging nested list in Mathematica


I have a list:
list1 = {{{3, 3, 3, 3}, {1, 1, 1, 1}, {2, 2, 2, 2}}, {{3, 3, 3, 3}, {1, 1, 1, 1}, {2, 2, 2, 2}}};
From the list above I would like to obtain a following list:
{{{3, 3, 3, 3, 3, 3, 3, 3}, {1, 1, 1, 1, 1, 1, 1, 1}, {2, 2, 2, 2, 2, 2, 2, 2}}}
I have tried to use ArrayReshape, but the resulting list is not what I desired:
list2 = ArrayReshape[list1, {1, 3, 8}]
{{{3, 3, 3, 3, 1, 1, 1, 1}, {2, 2, 2, 2, 3, 3, 3, 3}, {1, 1, 1, 1, 2, 2, 2, 2}}}
*edit
Desired solution should generalize to lists with all inputs being different.
**edit
Example of a list for general case:
{{{a1,a2,a3},{b1,b2,b3},{c1,c2,c3}},{{d1,d2,d3},{e1,e2,e3},{f1,f2,f3}}}
And desired outcome:
{{{a1,a2,a3,d1,d2,d3},{b1,b2,b3,e1,e2,e3},{c1,c2,c3,f1,f2,f3}}}


Solution

  • Try

    list1 = {{{3,3,3,3},{1,1,1,1},{2,2,2,2}},{{3,3,3,3},{1,1,1,1},{2,2,2,2}}}; 
    f[v_]:=Table[v,{Count[Flatten[list1],v]}]
    Map[f,DeleteDuplicates[Flatten[list1]]]
    

    which instantly returns

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

    EDIT

    Is there any chance that this is what you were looking for?

    list1 = {{{1,2,3,4},{5,6,7,8},{9,10,11,12}},
             {{13,14,15,16},{17,18,19,20},{21,22,23,24}}};
    MapThread[Join,list1]
    

    which returns

    {{1,2,3,4,13,14,15,16},{5,6,7,8,17,18,19,20},{9,10,11,12,21,22,23,24}}
    

    Just guessing