Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

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

Search the Archive

Re: All Possible Combinations from N Sublists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg97672] Re: [mg97639] All Possible Combinations from N Sublists
  • From: Bob Hanlon <hanlonr at cox.net>
  • Date: Wed, 18 Mar 2009 04:55:36 -0500 (EST)
  • Reply-to: hanlonr at cox.net

myList = {{1, 2, 3}, {e, f}, {g, h}};

Flatten[Outer[List, Sequence @@ myList], Length[myList] - 1]

{{1, e, g}, {1, e, h}, {1, f, g},
    {1, f, h}, {2, e, g}, {2, e, h},
    {2, f, g}, {2, f, h}, {3, e, g},
    {3, e, h}, {3, f, g}, {3, f, h}}

Tuples[myList]

{{1, e, g}, {1, e, h}, {1, f, g},
    {1, f, h}, {2, e, g}, {2, e, h},
    {2, f, g}, {2, f, h}, {3, e, g},
    {3, e, h}, {3, f, g}, {3, f, h}}

% == %%

True



Bob Hanlon


On Tue, Mar 17, 2009 at 6:54 AM , Donald DuBois wrote:

> Given a list of N sublists, how can I produce all possible 
> combinations where the first
> element comes from the first sublst, the second element from the 
> second sublist etc.
>
> For instance,
> myList = {{1, 2, 3}, {e, f}, {g, h}}
>
> the result should be,
>
> {{1, e, g}, {1, e, h}, {1, f, g}, {1, f, h}, {2, e, g}, {2, e, h}, {2, 
> f,   g}, {2, f, h}, {3, e, g}, {3, e, h}, {3, f, g}, {3, f, h}}
>
> Thank you.


  • Prev by Date: Wolfram WorkBench and iDisk
  • Next by Date: Re: Calling functions with huge arguments
  • Previous by thread: Re: All Possible Combinations from N Sublists
  • Next by thread: Re: Re: All Possible Combinations from N Sublists