Re: List manipulation
- To: mathgroup at smc.vnet.net
- Subject: [mg7543] Re: [mg7390] List manipulation
- From: Dick Zacher <dick at loc3.tandem.com>
- Date: Fri, 13 Jun 1997 19:37:34 -0400 (EDT)
- Organization: Tandem Computers
- Sender: owner-wri-mathgroup at wolfram.com
seanross at worldnet.att.net wrote:
>
> V. Nandagopal wrote:
> >
> > Hello,
> >
> > I have a list X={1,2,3,4,5,6,7,8}. How do I get a list of the
> > folllowing kind:
> >
> > S={{1,2,3,4}, {1,2,3,5}, {1,2,3,6}, .....}.
> >
> > S will have 8-choose-2 = 70 elements. Each element of S should have length
> > 4.
> >
> > Is there any function which does such list making? Something of
> > the form
> >
> > ?????[list, n]
> >
> > Thanks in advance.
> >
> > V. Nandagopal
> > School of Mathematics
> > Tata Institute of Fundamental Research
> > Colaba, Bombay 400 005, India
> >
> > e-mail: nandgopa at math.tifr.res.in
>
> Try using the Table command. Something like
>
> Table[Join[Take[mylist,3],{i}],{i,4,whatever}]
>
> You may have to play around with the curly braces etc or replace the
> start and end values of i with something like
> {i,mylist[[4]],mylist[[Length[mylist]]]} so it would work on any list.
-----------------------------
Or you can use the KSubsets function from the standard package
DiscreteMath`Combinatorica` as follows:
In[1]:=
<<DiscreteMath`Combinatorica`
In[2]:=
x={1,2,3,4,5,6,7,8}
Out[2]=
{1,2,3,4,5,6,7,8}
In[3]:=
s = KSubsets[x,4]
Out[3]=
{{1, 2, 3, 4}, {1, 2, 3, 5}, {1, 2, 3, 6}, {1, 2, 3, 7},
{1, 2, 3, 8}, {1, 2, 4, 5}, {1, 2, 4, 6}, {1, 2, 4, 7},
{1, 2, 4, 8}, {1, 2, 5, 6}, {1, 2, 5, 7}, {1, 2, 5, 8},
{1, 2, 6, 7}, {1, 2, 6, 8}, {1, 2, 7, 8}, {1, 3, 4, 5},
{1, 3, 4, 6}, {1, 3, 4, 7}, {1, 3, 4, 8}, {1, 3, 5, 6},
{1, 3, 5, 7}, {1, 3, 5, 8}, {1, 3, 6, 7}, {1, 3, 6, 8},
{1, 3, 7, 8}, {1, 4, 5, 6}, {1, 4, 5, 7}, {1, 4, 5, 8},
{1, 4, 6, 7}, {1, 4, 6, 8}, {1, 4, 7, 8}, {1, 5, 6, 7},
{1, 5, 6, 8}, {1, 5, 7, 8}, {1, 6, 7, 8}, {2, 3, 4, 5},
{2, 3, 4, 6}, {2, 3, 4, 7}, {2, 3, 4, 8}, {2, 3, 5, 6},
{2, 3, 5, 7}, {2, 3, 5, 8}, {2, 3, 6, 7}, {2, 3, 6, 8},
{2, 3, 7, 8}, {2, 4, 5, 6}, {2, 4, 5, 7}, {2, 4, 5, 8},
{2, 4, 6, 7}, {2, 4, 6, 8}, {2, 4, 7, 8}, {2, 5, 6, 7},
{2, 5, 6, 8}, {2, 5, 7, 8}, {2, 6, 7, 8}, {3, 4, 5, 6},
{3, 4, 5, 7}, {3, 4, 5, 8}, {3, 4, 6, 7}, {3, 4, 6, 8},
{3, 4, 7, 8}, {3, 5, 6, 7}, {3, 5, 6, 8}, {3, 5, 7, 8},
{3, 6, 7, 8}, {4, 5, 6, 7}, {4, 5, 6, 8}, {4, 5, 7, 8},
{4, 6, 7, 8}, {5, 6, 7, 8}}
In[4]:=
Length[%]
Out[4]=
70
--
-----------------------------
Dick Zacher
Performance Engineering Dept., Tandem Computers
zacher_dick at tandem.com phone: 408-285-5746 fax: 408-285-7079