4
$\begingroup$

I have two lists let say

listF = {{7, 2}, {2, 6}, {8, 1}, {1, 7}, {11, 8}, {6, 11}};

and

newD = {{{2, 7}, {7, 9}, {9, 2}}, {{7, 2}, {2, 6}, {6, 7}}, {{7, 
 2}, {2, 6}, {6, 7}}, {{11, 6}, {6, 2}, {2, 11}}, {{8, 1}, {1, 
 7}, {7, 8}}, {{11, 1}, {1, 8}, {8, 11}}, {{1, 5}, {5, 7}, {7, 
 1}}, {{8, 1}, {1, 7}, {7, 8}}, {{11, 1}, {1, 8}, {8, 11}}, {{11, 
 8}, {8, 6}, {6, 11}}, {{11, 6}, {6, 2}, {2, 11}}, {{11, 8}, {8, 
 6}, {6, 11}}};

Question: How can I delete the parts listF from newD disregarding of the order of elements in the sub-list of listF. For example, I need to delete parts from newD that whether are in the form of {2,7} or {7,2}. I would prefer to Apply Alternative command but any solution would be appreciated.

$\endgroup$
  • $\begingroup$ You could use something like DeleteCases[newD, Alternatives@@Join[listF, Reverse[listF, {2}]]] (untested). $\endgroup$ – Leonid Shifrin May 2 '16 at 13:16
  • $\begingroup$ @LeonidShifrin, Thanks for the help.Alternatives parts works but there is a trick that newD has sub-lists and the code needs to apply to those sub-lists. $\endgroup$ – Mery May 2 '16 at 13:25
  • $\begingroup$ @Mery Tak a look at level spec for DeleteCases in documentation. Or you could use: newD /. Thread[Join[listF, Reverse[listF, 2]] -> Nothing] $\endgroup$ – Kuba May 2 '16 at 13:35
  • 1
    $\begingroup$ @LeonidShifrin,@Kuba, Thanks. I got DeleteCases[newD, Alternatives @@ Join[listF, Reverse[listF, {2}]], 2]. that woks. $\endgroup$ – Mery May 2 '16 at 13:40
  • $\begingroup$ A minor variant DeleteCases[newD, # | Reverse[#, {2}] , {2}] &@ (Alternatives @@ listF). To delete just one element from each sublist, maybe: Function[x, DeleteCases[x, # | Reverse[#, {2}] , {1}, 1] &@ (Alternatives @@ listF)] /@ newD $\endgroup$ – user1066 May 2 '16 at 22:42
4
$\begingroup$

You may use OrderlessPatternSequence and DeleteCases.

Build a set of Alternatives with OrderlessPatternSequence and Map DeleteCases over the sublists.

DeleteCases[Alternatives @@ ({OrderlessPatternSequence @@ #} & /@ listF)] /@ newD
(*
{
  {{7, 9}, {9, 2}},    {{6, 7}}, {{6, 7}}, 
  {{2, 11}},           {{7, 8}}, 
  {{11, 1}},           {{1, 5}, {5, 7}}, 
  {{7, 8}},            {{11, 1}}, 
  {{8, 6}},            {{2, 11}}, 
  {{8, 6}}
}
*)

Hope this helps.

$\endgroup$
  • 1
    $\begingroup$ Thank you. I did not know about OrderlessPatternSequence command. Looks great for my future works. $\endgroup$ – Mery May 7 '16 at 4:37
3
$\begingroup$
Table[Select[newD[[i]], 
  Complement[{#}, listF] != {} && Complement[{Reverse@#}, listF] != {} &]
, {i, Length[newD]}]

{{{7, 9}, {9, 2}}, {{6, 7}}, {{6, 7}}, {{2, 11}}, {{7, 8}}, {{11, 1}}, {{1, 5}, {5, 7}}, {{7, 8}}, {{11, 1}}, {{8, 6}}, {{2, 11}}, {{8, 6}}}

Remove only one element

Table[Select[newD[[i]],Complement[{#}, listF] == {} || 
  Complement[{Reverse@#}, listF] == {} &], {i, Length[newD]}];
del = %[[All, {1}]] (*choose the first repetition*)

Complement[newD[[#]], del[[#]]] & /@ Range[Length[newD]]

{{{7, 9}, {9, 2}}, {{2, 6}, {6, 7}}, {{2, 6}, {6, 7}}, {{2, 11}, {6, 2}}, {{1, 7}, {7, 8}}, {{8, 11}, {11, 1}}, {{1, 5}, {5, 7}}, {{1, 7}, {7, 8}}, {{8, 11}, {11, 1}}, {{6, 11}, {8, 6}}, {{2, 11}, {6, 2}}, {{6, 11}, {8, 6}}}

$\endgroup$
  • $\begingroup$ Thanks for the code. Is there any way that I can delete only one elements from each sub-list in newD? I mean, I need each sub-list of newD have two parts. $\endgroup$ – Mery May 2 '16 at 13:44
3
$\begingroup$
foo[x_] := Sequence[x, Reverse@x];
DeleteCases[newD, Alternatives @@ (foo /@ listF), 2]

{{{7, 9}, {9, 2}}, {{6, 7}}, {{6, 7}}, {{2, 11}}, {{7, 8}}, {{11, 1}},
{{1, 5}, {5, 7}}, {{7, 8}}, {{11, 1}}, {{8, 6}}, {{2, 11}}, {{8, 6}}}

fun = ## & @@ (## &[{##}, {#2, #}] & @@@ #) &;
DeleteCases[newD, Alternatives@fun@listF, 2]

{{{7, 9}, {9, 2}}, {{6, 7}}, {{6, 7}}, {{2, 11}}, {{7, 8}}, {{11, 1}},
{{1, 5}, {5, 7}}, {{7, 8}}, {{11, 1}}, {{8, 6}}, {{2, 11}}, {{8, 6}}}

Update:

Is there any way that I can delete only one elements from each sub-list in newD

Yes... use the fourth argument of DeleteCases:

DeleteCases[#, Alternatives@fun@listF, 2, 1] & /@ newD

{{{7, 9}, {9, 2}}, {{2, 6}, {6, 7}}, {{2, 6}, {6, 7}}, {{6, 2}, {2, 11}},
{{1, 7}, {7, 8}}, {{11, 1}, {8, 11}}, {{1, 5}, {5, 7}}, {{1, 7}, {7, 8}},
{{11, 1}, {8, 11}}, {{8, 6}, {6, 11}}, {{6, 2}, {2, 11}}, {{8, 6}, {6, 11}}}

$\endgroup$
  • 1
    $\begingroup$ That's elegant, +1 $\endgroup$ – ciao May 7 '16 at 2:59
  • $\begingroup$ @kglr, Thank you $\endgroup$ – Mery May 7 '16 at 4:35

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.