2
$\begingroup$

I'm writing a function with two inputs input1 and input2 as below.
Inside the input1 there are probably a pair of sublists which has one element is from input2 and the other element is same in each list of the pair.
This is an example of the mention pair: {"LNL", "OUT"}, {"CPC", "OUT"}.
Note that the order in the sublist pair could also be {"OUT","LNL"}, {"CPC", "OUT"}.
The deleted sublist from each pair could be any sublist. Just one reguirement is that keep one sublist remaning in the pair.

input1 = {{"LPL", "IN"}, {"GN", "LPL"}, {"LNL", "OUT"}, {"CPC", 
    "OUT"}, {"CNC", "GN"}};
input2 = {"LNL", "CPC"};
myfunction[input1_, input2_] := Module[{},
  ]

This is the expected output:

output = {{"LPL", "IN"}, {"GN", "LPL"}, {"CPC", 
   "OUT"}, {"CNC", "GN"}}

I was trying to list all possible cases but due to the same element in each sublist of the pair could be a lot so I gave up doing that way.

Forgot to ask question: how can I do it without having to list all same element in each sublist?

$\endgroup$

2 Answers 2

2
$\begingroup$
ClearAll[myFunction]
myFunction[input1_, input2_] := Catenate @ Values @
   GroupBy[input1, DisjointQ[input2, #] &, 
    DeleteDuplicatesBy[DeleteCases[Alternatives @@ input2]]]

Examples:

input1 = {{"LPL", "IN"}, {"GN", "LPL"}, {"LNL", "OUT"}, {"CPC", "OUT"}, {"CNC", "GN"}};
input2 = {"LNL", "CPC"};

myFunction[input1, input2]
{{"LPL", "IN"}, {"GN", "LPL"}, {"CNC", "GN"}, {"LNL", "OUT"}}
input1b = {{"LPL", "IN"}, {"GN", "LPL"}, {"LNL", "OUT"}, {"CPC", "OUT"}, 
   {"CNC", "GN"}, {"CPC", "FOO"}, {"LNL", "BAR"}};

myFunction[input1b, input2]
{{"LPL", "IN"}, {"GN", "LPL"}, {"CNC", "GN"}, {"LNL", "OUT"}, 
 {"CPC", "FOO"}, {"LNL", "BAR"}}
$\endgroup$
1
  • $\begingroup$ Nice, I knew that there would a way to write this short but couldn't make it. No wonder why as I haven't used Catenate , Values and DisjointQ before. $\endgroup$
    – hana
    Jan 26, 2022 at 20:49
2
$\begingroup$

Here is an example using pattern matching:

We first define a pattern that that picks out all pairs with one common element. Then we use this pattern and check if the remaining elements are in input2. Finally we delete the first pair in input1:

With the data:

input1 = {{"LPL", "IN"}, {"GN", "LPL"}, {"LNL", "OUT"}, {"CPC", 
    "OUT"}, {"CNC", "GN"}};
input2 = {"LNL", "CPC"};

and the function:

myfunction[input_1,input2_]:=Module[{},
pattern = ({{x1_, x2_}, {x1_, x3_}} | {{x1_, x2_}, {x3_, 
      x1_}} | {{x2_, x1_}, {x1_, x3_}} | {{x2_, x1_}, {x3_, x1_}});
del = Cases[subs, 
   pattern /; MemberQ[input2, x2] && MemberQ[input2, x3]];
DeleteCases[input1, Alternatives @@ del[[All, 1]]]
]

we get the output:

myfunction[input1, input2]

(* {{"LPL", "IN"}, {"GN", "LPL"}, {"CPC", "OUT"}, {"CNC", "GN"}} *)
$\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.