Message Boards Message Boards

0
|
7657 Views
|
5 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Construct / re-arrange some lists?

Posted 7 years ago

Hi all,

I am trying to re-arranging some lists with some variables. Following code:

lista = {{a, b, c}, {d, e, f, g}};
listb = {{h, i, j}, {k, l, m}};
sol = Flatten[Outer[List, lsta, lstb], 3]

With my Flatten[....] command I can't get unfortunately the right re-arangement. My goal is that the first bracket of lista shall only be processed with the first bracket of listb:

{{a, h}, {a, i}, {a, j}, {b, h}, {b, i}, {b, j}, {c, h}, {c, i}, {c, 
  j}}

as well as only the 2. bracket of lista with the 2. bracket of list b and so on.....

{{d, k}, {d, l}, {d, m}, {e, k}, {e, l}, {e, m}, {f, k}, {f, l}, {f, 
  m}, {g, k}, {g, l}, {g, m}}

Do you have some ideas or hints how I can achieve that?

POSTED BY: Nikki Peter
5 Replies

I'm not sure if this is what you are asking:

In[1]:= Map[Flatten[Outer[List,lista,#],2]&,listb]

Out[1]= {{{a,h},{a,i},{a,j},{b,h},{b,i},{b,j},{c,h},{c,i},{c,j},{d,h},{d,i},{d,j},{e,h},{e,i},{e,j},{f,h},{f,i},{f,j},{g,h},{g,i},{g,j}},
         {{a,k},{a,l},{a,m},{b,k},{b,l},{b,m},{c,k},{c,l},{c,m},{d,k},{d,l},{d,m},{e,k},{e,l},{e,m},{f,k},{f,l},{f,m},{g,k},{g,l},{g,m}}}
POSTED BY: Gustavo Delfino
Posted 7 years ago

Hi Gustavo,

thanks for your help. Unfortunalety not. It is quite he same...For example

{d, e, f, g} 

shall only be processed with

{k, l, m}

and that results

{{d, k}, {d, l}, {d, m}, {e, k}, {e, l}, {e, m}, {f, k}, {f, l}, {f, 
  m}, {g, k}, {g, l}, {g, m}}

I will keep trying.....

POSTED BY: Nikki Peter

What about applying the function Table[]:

In[1]:= lista = {{a, b, c}, {d, e, f, g}, {n, o}};
listb = {{h, i, j}, {k, l, m}, {s, t, p, q}};

In[3]:= Flatten[
Table[{lista[[ii, jj]], listb[[ii, kk]]}, {ii, Length[lista]}, {jj, 
Length[lista[[ii]]]}, {kk, Length[listb[[ii]]]}], 2]

Out[3]= {{a, h}, {a, i}, {a, j}, {b, h}, {b, i}, {b, j}, {c, h}, {c,i}, {c, j},
{d, k}, {d, l}, {d, m}, {e, k}, {e, l}, {e, m}, {f, k}, {f, l}, {f, m}, {g, k}, {g, l}, {g, m},
{n, s}, {n, t}, {n, p}, {n, q}, {o, s}, {o, t}, {o, p}, {o, q}}

How about this:

In[1]:= Thread[f[lista, listb]] /. f[a_List, b_List] :> Flatten[Outer[List, a, b], 1]

Out[1]= {{{a,h}, {a,i}, {a,j}, {b,h}, {b,i}, {b,j}, {c,h}, {c,i}, {c,j}},
         {{d,k}, {d,l}, {d,m}, {e,k}, {e,l}, {e,m}, {f,k}, {f,l}, {f,m}, {g,k}, {g,l}, {g,m}}}
POSTED BY: Gustavo Delfino
Posted 7 years ago

Hi, many thanks @ Valeriu and Gustavo!

That's what I have searched for....

POSTED BY: Nikki Peter
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract