MathGroup Archive 1998

[Date Index] [Thread Index] [Author Index]

Search the Archive

SUMMARY: How {{a,b,c},{1,2,3,4,5,6,7}}-->{{a,1},{b,2},{c,3},{a,4},...,{a,7},{b,1},...}?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg14317] SUMMARY: How {{a,b,c},{1,2,3,4,5,6,7}}-->{{a,1},{b,2},{c,3},{a,4},...,{a,7},{b,1},...}?
  • From: Michael Kubovy <mk9y at virginia.edu>
  • Date: Tue, 13 Oct 1998 01:21:30 -0400
  • Organization: University of Virginia
  • Sender: owner-wri-mathgroup at wolfram.com

Dear Friends,

I asked how to go from (e.g.)
{{a,b,c},{1,2,3,4,5,6,7}} to
{{a,1},{b,2},{c,3},{a,4},...,{a,7},{b,1},...} ?

A number of people offered solutions. They vary in style and technique.
I thought the comparison might be of general interest.

*********
(1) Bruce.Fast at Colorado.EDU

mix[x_List] := Module[ {m=Map[Length,x],mx},
                mx=Max[m];
                Table[x[[j,1+Mod[i-1,m[[j]]]]],
                        {i,mx}, {j,Length[m]}]
                ]

In[7]:= mix[ {{a,b},{1,2,3}} ]
Out[7]= {{a, 1}, {b, 2}, {a, 3}}

In[8]:= mix[  {{a,b}, {1,2,3}, {v,w,x,y,z}} ] Out[8]= {{a, 1, v}, {b, 2,
w}, {a, 3, x}, {b, 1, y}, {a, 2, z}}

*********
(2) Levent Kitis <lk3a at esmeralda.mech.virginia.edu>

Tuples[list_List] :=
 Module[{m, n}, 
   {m, n} = Length /@ list;
   Which[ m < n, 
          Transpose[{Take[Join @@ Table[list[[1]], {Ceiling[n/m]}], n],
list[[2]]}],
          m > n, Transpose[{list[[1]], Take[Join @@ Table[list[[2]],
{Ceiling[m/n]}], m]}],
          m == n, Transpose[list]
        ]
       ]

Examples:

In[2]:= x = {{a, b}, {1, 2, 3, 4}};

In[3]:= Tuples[x]
Out[3]= {{a, 1}, {b, 2}, {a, 3}, {b, 4}}

In[4]:= x =  {{1, 2, 4}, {a, b}};

In[5]:= Tuples[x]
Out[5]= {{1, a}, {2, b}, {4, a}}

In[6]:= x = {{1, 2, 4}, {1, 2, 4}};

In[7]:= Tuples[x]
Out[7]= {{1, 1}, {2, 2}, {4, 4}}

In[8]:= x = {{1, 2, 3, 4, 5, 5, 6, 6, 7, 8}, {a, b, c}};

In[9]:= Tuples[x]
Out[9]= {{1, a}, {2, b}, {3, c}, {4, a}, {5, b}, {5, c}, {6, a}, {6, b},
{7, c}, {8, a}}

*********
(3) Selwyn Hollis <shollis at peachnet.campus.mci.net>

pairup[{a_List,b_List}]:= Module[{aa=a},
           While[Length[aa]<Length[b], aa=Join[aa,a]];
           aa=Delete[aa,Table[{i},{i, Length[b]+1, Length[aa]}]];
            Transpose[{aa,b}]]

For example,

        pairup[{{a,b,c},{1,2,3,4,5,6,7,8}}]

gives

       {{a,1},{b,2},{c,3},{a,4},{b,5},{c,6},{a,7},{b,8}}

*********
(4) Hans Staugaard <hans.staugaard at get2net.dk>

First I define a function pad:

pad[v:{__},n_Integer]:=         
  Take[Flatten[Table[v,{Ceiling[n/Length[v]]}]],n] /; n>Length[v]
pad[v:{__},n_Integer]:=
  v /; n=Length[v]

and then

maketuples[m:{{__}..}]:=
  With[{n=Max[Length[#]&/@m]},Transpose[pad[#,n]&/@m]]

this function should do the job, and will also work on things like

{{a,b},{1,2,3},{A,B,C,D}} yielding {{a,1,A},{b,2,B},{a,3,C},{b,1,D}}

*********
(5) "Ersek, Ted R" <ErsekTR at navair.navy.mil>

Let me assume you want to cycle through the first list until it's as
long as the second list.  Consider 'data' below.

In[1]:=
data={{a,b,c,d,e,f},{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,2
2,23}};

Using the lines below you can make a new list by cycling through the
first list the right number of times.  The you can use Transpose.

In[2]:=
l1=Table[lst, {Floor[23/Length[lst]]} ] Out[2]=
{{a,b,c,d,e,f},{a,b,c,d,e,f},{a,b,c,d,e,f}}

In[3]:=
l2=Take[lst, {1,Mod[23,Length[lst]]} ] Out[3]=
{a,b,c,d,e}

In[4]:=
l3=Flatten at Join[l1,l2]
Out[4]=
{a,b,c,d,e,f,a,b,c,d,e,f,a,b,c,d,e,f,a,b,c,d,e}

In[5]:=
Transpose[{l3,Part[data,2]}]
Out[5]=
{{a,1},{b,2},{c,3},{d,4},{e,5},{f,6},{a,7},{b,8},{c,9},{d,10},{e,11},{f,12},
{a,13},{b,14},{c,15},{d,16},{e,17},{f,18},{a,19},{b,20},{c,21},{d,22},{e,23}
}

Thanks to all,
|\  /|  / Michael Kubovy, Professor of Psychology, Dept of Psychology |
\/ | /  Univ of Virginia, Gilmer Hall, Charlottesville, VA 22903-2477 |
|/\  office (B011): 804-982-4729, lab (B019): -4751, fax: -4766  |    |
\ HTTP://www.virginia.edu/~mklab/


  • Prev by Date: Re: How: {{a,b},{1,2,3}} --> {{a,1},{b,2},{a,3}} ?
  • Next by Date: Re: [Q] Help with PrimitiveRoot
  • Previous by thread: Re: I Want More Digits in TraditionalForm
  • Next by thread: Two programming challenges