Re: Combinations of two lists
- To: mathgroup at smc.vnet.net
- Subject: [mg41802] Re: [mg41750] Combinations of two lists
- From: Ken Levasseur <klevasseur at mac.com>
- Date: Fri, 6 Jun 2003 09:50:34 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
John:
You can do it with Outer:
In[7]:=
list1={x1,y1,z1}
list2={x2,y2,z2}
Out[7]=
{x1, y1, z1}
Out[8]=
{x2, y2, z2}
In[9]:=
Outer[List,list1,list2]
Out[9]=
{{{x1, x2}, {x1, y2}, {x1, z2}},
{{y1, x2}, {y1, y2}, {y1, z2}},
{{z1, x2}, {z1, y2}, {z1, z2}}}
The basic result is an array of pairs but you can get simply a list of pairs
by using Flatten:
In[10]:=
Flatten[Outer[List,list1,list2],1]
Out[10]=
{{x1, x2}, {x1, y2}, {x1, z2}, {y1, x2}, {y1, y2}, {y1, z2},
{z1, x2}, {z1, y2}, {z1, z2}}
Ken Levasseur
Math. Sci.
UMass Lowell
> From: "John C. Erb, Ph.D." <John_C_Erb at prodigy.net>
To: mathgroup at smc.vnet.net
> Date: Wed, 04 Jun 2003 08:34:35 -0400 (EDT)
> To: mathgroup at smc.vnet.net
> Subject: [mg41802] [mg41750] Combinations of two lists
>
> Hello,
>
> A simple example of what I would like to do is:
>
> list1={x1,y1,z1}
> list2={x2,y2,z2}
>
> pair up the two lists to get all possible combinations
>
> {x1,x2},{y1,y2},{z1,z2}
> {x1,x2},{y1,z2},{z1,y2}
> {x1,y2},{y1,x2},{z1,z2}
> {x1,y2},{y1,z2},{z1,x2}
> {x1,z2},{y1,x2},{z1,y2}
> {x1,z2},{y1,y2},{z1,x2}
>
> I would like a general way of telling how many ways
> I can match up the two lists as shown above, and
> optionally print out the combinations.
>
> Thank you,
> John C. Erb
> email: John_C_Erb at prodigy.net
>