3
$\begingroup$

My inputs are 3 lists of unequal length:

A={a,b,c,d}
B={i,j,k}
C={v,w,x,y,z}

And I want to find the combine set X which looks like

X={{v,b},{w,a},{x,i},{y,j},{c,k},{d,z}}

i.e. the each sub-list should be of 2 elements and each of its elements should be from different lists A, B and C.

After this I would like to find all such possible X obtained from a given A, B and C.

$\endgroup$
1
  • $\begingroup$ Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. $\endgroup$
    – bbgodfrey
    Jul 16 at 13:12
5
$\begingroup$
lists = {{a, b, c, d}, {i, j, k}, {v, w, x, y, z}};

pairs = Join @@ Tuples /@ Subsets[lists, {2}]
(*    {{a, i}, {a, j}, {a, k}, {b, i}, {b, j}, {b, k}, {c, i},
       {c, j}, {c, k}, {d, i}, {d, j}, {d, k}, {a, v}, {a, w},
       {a, x}, {a, y}, {a, z}, {b, v}, {b, w}, {b, x}, {b, y},
       {b, z}, {c, v}, {c, w}, {c, x}, {c, y}, {c, z}, {d, v},
       {d, w}, {d, x}, {d, y}, {d, z}, {i, v}, {i, w}, {i, x},
       {i, y}, {i, z}, {j, v}, {j, w}, {j, x}, {j, y}, {j, z},
       {k, v}, {k, w}, {k, x}, {k, y}, {k, z}}                    *)

XX = Select[Subsets[pairs, {Length[Flatten[lists]]/2}],
            DuplicateFreeQ@*Flatten]
(*    {{{a, i}, {b, v}, {c, w}, {d, x}, {j, y}, {k, z}},
       {{a, i}, {b, v}, {c, w}, {d, x}, {j, z}, {k, y}},
       ...
       {{d, k}, {a, z}, {b, y}, {c, x}, {i, v}, {j, w}},
       {{d, k}, {a, z}, {b, y}, {c, x}, {i, w}, {j, v}}}    *)

There are 1440 such lists X. If you need them in canonical order, they need to be sorted at levels 0, 1, and 2:

XXs = Map[Sort, XX, {0, 2}]
(*    {{{a, i}, {b, v}, {c, w}, {d, x}, {j, y}, {k, z}},
       {{a, i}, {b, v}, {c, w}, {d, x}, {j, z}, {k, y}},
       ...
       {{a, z}, {b, y}, {c, x}, {d, k}, {i, v}, {j, w}},
       {{a, z}, {b, y}, {c, x}, {d, k}, {i, w}, {j, v}}}    *)

in which your original X appears at position 665:

X = {{v, b}, {w, a}, {x, i}, {y, j}, {c, k}, {d, z}};
Position[XXs, Map[Sort, X, {0, 1}]]
(*    {{665}}    *)
$\endgroup$
1
  • $\begingroup$ Thank you so much! $\endgroup$
    – jar-
    Jul 17 at 16:42
3
$\begingroup$

Try

A={a,b,c,d};
B={i,j,k};
cC={v,w,x,y,z};(*C is a predefined Mathematica name*)
Join[Tuples[{A,B}],Tuples[{A,cC}],
  Tuples[{B,A}],Tuples[{B,cC}],
  Tuples[{cC,A}],Tuples[{cC,B}]]
$\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.