Message Boards Message Boards

0
|
3429 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How do I construct tuples from lists?

Posted 11 years ago

I have n short lists, and I want to construct the list of all the n-tuples that can be constructed taking the i'th element from the i'th list. Eg From

{1,2},{3},{4,5} I want to construct {{1,3,4},{1,3,5},{2,3,4},{2,3,5}}

I was hoping some combination of Outer and Flatten might do it, but I can't make that work. Any ideas?

To illustrate my difficulty,

In[151]:= Outer[List, {1}, {2}, {3, 4}] generates Out[151]= {{{{1, 2, 3}, {1, 2, 4}}}}

whereas

In[152]:= Outer[List, {1}, {2, 3}, {4}] generates Out[152]= {{{{1, 2, 4}}, {{1, 3, 4}}}}

Jim Simons

POSTED BY: Jim Simons
2 Replies

Try this:

a = {1, 2}; b = {3}; c = {4, 5};
Tuples[{a, b, c}]
(* {{1, 3, 4}, {1, 3, 5}, {2, 3, 4}, {2, 3, 5}}  *)
POSTED BY: Nasser M. Abbasi
Posted 11 years ago

Marvelous, thanks you. I'd missed that second usage of Tuples.

Jim

POSTED BY: Jim Simons
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract