9
$\begingroup$

Some time ago I asked the question about Deleting list members by rule and received a variety of responses.

I have a slightly different data set and here wish to delete list members whose third and fifth elements are "xx" but otherwise are identical to the other members of that element:

lis = {{"a", "b", "c", "d", "e", "f"}, 
       {"g", "h", "i", "j", "l", "m"}, 
       {"a", "b", "xx", "d", "xx", "f"}, 
       {"o", "p", "q", "r", "s", "t"}};

to give:

res = {{"a", "b", "c", "d", "e", "f"}, 
       {"g", "h", "i", "j", "l", "m"},    
       {"o", "p", "q", "r", "s", "t"}}

Using @eyorble's suggestion, I tried:

lis /. {a___, 
Alternatives[
PatternSequence[{b_, c_, d_, e_, f_, g_}, {b_, c_, "xx", e_, "xx",
   g_}], PatternSequence[{b_, c_, "xx", e_, "xx", g_}, {b_, c_, 
  d_, e_, f_, g_}]], w___} :> {a, {b, c, d, e, f, g}, w}

...which fails.

$\endgroup$
1
  • $\begingroup$ A variation of the neat method given by @kglr (which also uses MapAt 'under the hood': DeleteDuplicatesBy[Query[Thread[{3,5}->("xx"&)]]]@lis $\endgroup$
    – user1066
    Sep 13, 2023 at 6:53

4 Answers 4

9
$\begingroup$
DeleteDuplicatesBy[MapAt["xx" &, {{3}, {5}}]] @ lis
{{"a", "b", "c", "d", "e", "f"},   
 {"g", "h", "i", "j", "l", "m"},   
 {"o", "p", "q", "r", "s", "t"}}
lis2 = Append[lis, {"z", "b", "xx", "d", "xx", "f"}];

DeleteDuplicatesBy[MapAt["xx" &, {{3}, {5}}]] @ lis2
{{"a", "b", "c", "d", "e", "f"},   
 {"g", "h", "i", "j", "l", "m"},   
 {"o", "p", "q", "r", "s", "t"},   
 {"z", "b", "xx", "d", "xx", "f"}}
$\endgroup$
6
$\begingroup$
lis = {{"a", "b", "c", "d", "e", "f"}, {"g", "h", "i", "j", "l", 
    "m"}, {"a", "b", "xx", "d", "xx", "f"}, {"o", "p", "q", "r", "s", 
    "t"}, {"z", "b", "xx", "d", "xx", "f"}};

comp = Complement[Range[6], {3, 5}]
First /@ GatherBy[lis, {#[[comp]] &}]

{{"a", "b", "c", "d", "e", "f"}, {"g", "h", "i", "j", "l", "m"}, {"o", "p", "q", "r", "s", "t"}, {"z", "b", "xx", "d", "xx", "f"}}


EDIT

To make this edit more general, the addendum will use the following list that could contain more duplicated entries matching the placeholder pattern:

lis = {{"a", "b", "c", "d", "e", "f"}, {"a", "b", "g", "d", "m", 
    "f"}, {"g", "h", "i", "j", "l", "m"}, {"a", "b", "xx", "d", "xx", 
    "f"}, {"o", "p", "q", "r", "s", "t"}, {"z", "b", "xx", "d", "xx", 
    "f"}};

GatherBy[lis, {#[[comp]] &}] /. {{k_List} :> 
   k, {g___, {_, _, "xx", _, "xx", _}, h___} -> Sequence @@ {g, h}}

{{"a", "b", "c", "d", "e", "f"}, {"a", "b", "g", "d", "m", "f"}, {"g", "h", "i", "j", "l", "m"}, {"o", "p", "q", "r", "s", "t"}, {"z",
"b", "xx", "d", "xx", "f"}}

where unduplicated entries (even those containing "xx" are retained). Only the placeholder matching any duplicated entry is filtered out.

$\endgroup$
6
$\begingroup$

Using the third argument of GroupBy:

lis = {{"a", "b", "c", "d", "e", "f"}, {"g", "h", "i", "j", "l", "m"}, 
      {"a", "b", "xx", "d", "xx", "f"}, {"o", "p", "q", "r", "s", "t"}, 
      {"z", "b", "xx", "d", "xx", "f"}};

Values@GroupBy[lis, #[[1]] &, First] (*Edit*)

(*{{"a", "b", "c", "d", "e", "f"}, {"g", "h", "i", "j", "l", "m"}, 
  {"o","p", "q", "r", "s", "t"}, {"z", "b", "xx", "d", "xx", "f"}}*)

An example suggested by Eldo:

lis = {{"a", "b", "c", "d", "e", "f"}, {"g", "h", "i", "j", "l", 
"m"}, {"a", "b", "xx", "d", "xx", "f"}, {"o", "p", "q", "r", "s", 
"t"}, {"z", "b", "xx", "d", "xx", "f"}, {"z", "b", "xx", "d", 
"xx", "f"}};

Values@GroupBy[lis, #[[1]] &, First]

(*{{"a", "b", "c", "d", "e", "f"}, {"g", "h", "i", "j", "l", "m"}, 
{"o","p", "q", "r", "s", "t"}, {"z", "b", "xx", "d", "xx", "f"}}*)
$\endgroup$
4
  • 3
    $\begingroup$ The question is about deleting any duplicates but with the exception of "placeholder" positions 3, 5 as interpreted by @kglr. Initially, I misunderstood it, so I will delete the comment. $\endgroup$
    – Syed
    Sep 11, 2023 at 18:40
  • $\begingroup$ Thanks, mate! :-) $\endgroup$ Sep 11, 2023 at 18:53
  • 1
    $\begingroup$ Does this function with lis = {{"a", "b", "c", "d", "e", "f"}, {"g", "h", "i", "j", "l", "m"}, {"a", "b", "xx", "d", "xx", "f"}, {"o", "p", "q", "r", "s", "t"}, {"z", "b", "xx", "d", "xx", "f"}, {"z", "b", "xx", "d", "xx", "f"}} ? $\endgroup$
    – eldo
    Sep 11, 2023 at 23:05
  • $\begingroup$ Thanks, @eldo! :-) $\endgroup$ Sep 12, 2023 at 0:36
1
$\begingroup$
la =
  {{"a", "b", "c", "d", "e", "f"},
   {"g", "h", "i", "j", "l", "m"},
   {"a", "b", "xx", "d", "xx", "f"},
   {"o", "p", "q", "r", "s", "t"}};

lb =
  {{"a", "b", "c", "d", "e", "f"},
   {"g", "h", "i", "j", "l", "m"},
   {"a", "b", "xx", "d", "xx", "f"},
   {"o", "p", "q", "r", "s", "t"},
   {"o", "p", "xx", "r", "xx", "t"}};

del[list_] :=
 With[{pattern = {{a_, b_, _, c_, _, d_}, p : {a_, b_, "xx", c_, "xx", d_}} :> p},
  With[{duplicates = Alternatives @@ Cases[Subsets[list, {2}], pattern]},
   Delete[list, Position[list, duplicates]]]]

del @ la

{{"a", "b", "c", "d", "e", "f"}, {"g", "h", "i", "j", "l", "m"}, {"o", "p", "q", "r", "s", "t"}}

del @ lb

{{"a", "b", "c", "d", "e", "f"}, {"g", "h", "i", "j", "l", "m"}, {"o", "p", "q", "r", "s", "t"}}

$\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.