2
$\begingroup$

I created with

Tuples[{{USD, EUR, JPY}, {UP, DOWN}}]

the following result:

{{USD, UP}, {USD, DOWN}, {EUR, UP}, {EUR, DOWN}, {JPY, UP}, {JPY, DOWN}}

I want to further have a 3 dimensional list of all combinations - but the same currency does not appear twice. Example output

{{{USD, UP}, {EUR, UP}, {JPY, UP}}, {{USD, UP}, {EUR, UP}, {JPY, DOWN}}, {{USD, UP}, {EUR, DOWN}, {JPY, UP}}, {{USD, UP}, {EUR, DOWN}, {JPY, DOWN}}, ... {{USD, DOWN}, {EUR, DOWN}, {JPY, DOWN}}}

Is there a simple way to do it?

$\endgroup$
2
$\begingroup$

Here is one way:

Tuples@Outer[List, {USD, EUR, JPY}, {UP, DOWN}]
(*
  {{{USD, UP}, {EUR, UP}, {JPY, UP}},
   {{USD, UP}, {EUR, UP}, {JPY, DOWN}},
   {{USD, UP}, {EUR, DOWN}, {JPY, UP}},
   {{USD, UP}, {EUR, DOWN}, {JPY, DOWN}},
   {{USD, DOWN}, {EUR, UP}, {JPY, UP}},
   {{USD, DOWN}, {EUR, UP}, {JPY, DOWN}},
   {{USD, DOWN}, {EUR, DOWN}, {JPY, UP}},
   {{USD, DOWN}, {EUR, DOWN}, {JPY, DOWN}}}
*)
$\endgroup$
  • $\begingroup$ I can't beat that. I tried. :-) $\endgroup$ – Mr.Wizard Jul 18 '14 at 13:12
  • $\begingroup$ @Mr.Wizard Thanks! $\endgroup$ – Michael E2 Jul 18 '14 at 13:19
0
$\begingroup$

Yes, one way is to use Tuples again after some judicious partitioning:

Tuples[Partition[Tuples[{{"USD", "EUR", "JPY"}, {"UP", "DOWN"}}], 2]]

Note the 2 is the length of the second set in the original Tuples so you could write your own function:

myTuples[list1_List, list2_List] := Tuples[Partition[Tuples[{list1, list2}], Length[list2]]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.