Re: All Possible Combinations from N Sublists
- To: mathgroup at smc.vnet.net
- Subject: [mg97676] Re: All Possible Combinations from N Sublists
- From: Szabolcs Horvát <szhorvat at gmail.com>
- Date: Wed, 18 Mar 2009 04:56:21 -0500 (EST)
- References: <gpnsej$gq4$1@smc.vnet.net>
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. > Tuples[] does exactly this (Outer[] is similar too, but a little bit of flattening would be needed with that). In[2]:= Tuples[{{1, 2, 3}, {e, f}, {g, h}}] Out[2]= {{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}}