4
$\begingroup$

Consider the following list:

list = {{meson1, p1}, {meson2, p2}, {meson3, p3}, {meson4, p4}}

I would like to organize it into two sub-sets, each with two elements, with no repetitions. Say,

listfinal = {{{{meson1,p1},{meson2,p2}},{{meson3,p3},{meson4,p4}}}, {{{meson1,p1},{meson3,p3}},{{meson2,p2},{meson4,p4}}}, ...}

where we have, e.g., the following first pair of 2-subsets:

{{{meson1,p1},{meson2,p2}},{{meson3,p3},{meson4,p4}}}

The subset {{meson4,p4},{meson2,p2}} is a duplicate of {{meson2,p2},{meson4,p4}}.

I know that there is a command

Subsets[list, {2}]

but it does not do exactly what I want: one more action is missing - I need to combine the elements of the 2-subsets such that I will get listfinal. Could you please tell me how to obtain listfinal?

P.S. In my real example, meson1, meson2, etc (but not p1,...), are strings, and it may be that "meson2" is the same as "meson1".

$\endgroup$

3 Answers 3

5
$\begingroup$

For the case in question only:

({#,Complement[list, #]}&/@Subsets[#, {2},3)&@list


(* {
    {{{meson1, p1}, {meson2, p2}}, {{meson3, p3}, {meson4, p4}}}, 
    {{{meson1, p1}, {meson3, p3}}, {{meson2, p2}, {meson4, p4}}}, 
    {{{meson1, p1}, {meson4, p4}}, {{meson2, p2}, {meson3, p3}}}
   } *)

Or

({#,Complement[list, #]}&/@Subsets[#, {2},Binomial[Length@#,2]/2])&@list

 (* {
    {{{meson1, p1}, {meson2, p2}}, {{meson3, p3}, {meson4, p4}}}, 
    {{{meson1, p1}, {meson3, p3}}, {{meson2, p2}, {meson4, p4}}}, 
    {{{meson1, p1}, {meson4, p4}}, {{meson2, p2}, {meson3, p3}}}
   } *)
({#,Complement[Range[4], #]}&/@Subsets[#,{2},Binomial[Length@#,2]/2])&@Range[4]

(* {
    {{1, 2}, {3, 4}}, 
    {{1, 3}, {2, 4}}, 
    {{1, 4}, {2, 3}}
   } *)
$\endgroup$
0
5
$\begingroup$
Needs["Combinatorica`"]

list = {{"meson1", p1}, {"meson2", p2}, {"meson3", p3}, {"meson4", 
    p4}};

Cases[KSetPartitions[list, 2], {Repeated[{_, _}, {2}]}]

{{{{"meson1", p1}, {"meson2", p2}}, {{"meson3", p3}, {"meson4", p4}}}, {{{"meson1", p1}, {"meson4", p4}}, {{"meson2", p2}, {"meson3", p3}}}, {{{"meson1", p1}, {"meson3", p3}}, {{"meson2", p2}, {"meson4", p4}}}}

$\endgroup$
2
$\begingroup$

Using SubsetCases (new in 12.1)

sub = Transpose @ Partition[SubsetCases[list, {_, _}, Overlaps -> True], 3]

{{{{meson1, p1}, {meson2, p2}}, {{meson2, p2}, {meson3, p3}}},
{{{meson1, p1}, {meson3, p3}}, {{meson2, p2}, {meson4, p4}}},
{{{meson1, p1}, {meson4, p4}}, {{meson3, p3}, {meson4, p4}}}}

The ordering of the right block is slightly different from the accepted result. If this is a problem we could do:

Transpose[{sub[[All, 1]], Reverse @ sub[[All, 2]]}]

{{{{meson1, p1}, {meson2, p2}}, {{meson3, p3}, {meson4, p4}}},
{{{meson1, p1}, {meson3, p3}}, {{meson2, p2}, {meson4, p4}}},
{{{meson1, p1}, {meson4, p4}}, {{meson2, p2}, {meson3, p3}}}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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