3
$\begingroup$

Simple question: I would like to dynamically specify a list of 3 elements, {x,y,z}. Then I'd like to find that list, or any of its permutations, in a parent data set. The parent data set is composed of lists which are at least 3 elements long (like mine), but could also be 4 or 5 elements long. I will then be dynamically deleting the entire part which has an occurrence of x,y,z or z,x,y etc. from the parent data set (list of lists).

The first way I tried doesn't work for obvious reasons (I've removed Dynamic functionality because it doesn't work anyway):

data={{0,0,0},{1,1,1},{1,2,3},{3,2,1,9}};
x=3;
y=1;
z=2;
DeleteCases[data,Permutations[{x,y,z}]]

As you can see, I want this to find and delete the 3rd and 4th elements, so that {{0,0,0},{1,1,1}} is returned. I'd also like to update {x,y,z} dynamically.

Are there any tricks to make this work? Or am I doing it entirely wrong? Thanks!

$\endgroup$

1 Answer 1

4
$\begingroup$

Here is a simple way. It's not particularly fast, but may be fast enough for your purposes.

data = {{0, 0, 0}, {1, 1, 1}, {1, 2, 3}, {3, 2, 1, 9}};
subseq = Sort@{3, 1, 2};
DeleteCases[data, {___, a_, b_, c_, ___} /; Sort[{a, b, c}] == subseq]
(* {{0, 0, 0}, {1, 1, 1}} *)
$\endgroup$

Your Answer

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

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