1
$\begingroup$

I have two lists:

 lst1 = {a,b,c,d,b,e,c}

and:

 lst2 = {b,c}

I am interested in removing all occurrences of lst2 from lst1, producing:

res = {a,d,b,e,c}

This is not exactly the same as Removing elements from a list which appear in another list; thank you for any thoughts.

$\endgroup$
4
  • 1
    $\begingroup$ lst1 //. {start___, Sequence @@ lst2, end___} :> {start, end} $\endgroup$ – Bob Hanlon Oct 17 '16 at 0:47
  • $\begingroup$ What if lst1 = {a,b,a,b,a} and lst2 = {a,b,a}? Should your output be {b,a} or {a,b} or { } ? $\endgroup$ – dan7geo Oct 17 '16 at 1:06
  • $\begingroup$ @dan7geo: it would be {}; thanks bob, will play around some more with it. $\endgroup$ – Suite401 Oct 17 '16 at 4:47
  • 2
    $\begingroup$ You need to specify in which way this question actually differs. $\endgroup$ – Yves Klett Oct 17 '16 at 9:54
1
$\begingroup$

Somehow this does not work (according to the documentation it should, or please tell me what I am doing wrong)

SequenceCases[lst1, lst2 ->{}, Overlaps -> True]

A work around would be first computing the positions to be deleted:

toBeDeleted = Flatten[Range[Sequence @@ #] & /@ SequencePosition[lst1, lst2, Overlaps -> True], 1]

And then delete these positions:

Delete[lst1, {#} & /@ toBeDeleted]

$\endgroup$

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