1
$\begingroup$

I have a large ordered list of lists of integers like this

set={{3,4,5,6,7,9},{1,2,3,4,5,6},{3,4,5,6,9,10},{3,4,5,6,11,12},{3,4,6,9,11,12},...}

and I would like to filter out the nested list based on two conditions. The conditions are two types of shorter nested lists

  • pairs of consecutive numbers that are not allowed anywhere in the nested list
  • pairs of consecutive numbers that must occur somewhere in the list

required={{3,4},{5,6},...}

notallowed={{1,2},{9,10},...}

The output in this case would be

{{3,4,5,6,7,9},{3,4,5,6,11,12}}
$\endgroup$
3
  • $\begingroup$ why do you need both notallowed and also allowed? Would not just using notallowed be enought to remove those unwanted? $\endgroup$
    – Nasser
    Commented Feb 19, 2018 at 23:45
  • $\begingroup$ No because allowed must be there all present. I can remove all notallowed but that does not guarantee there are all alowed (can be {3,4} but not {5,6}). $\endgroup$ Commented Feb 20, 2018 at 0:01
  • $\begingroup$ For your use case, what would the maximum integer be? Are the allowed and disallowed sets always pairs of consecutive numbers? Are your sublists always ordered? $\endgroup$
    – Carl Woll
    Commented Feb 20, 2018 at 0:54

1 Answer 1

0
$\begingroup$

You can use SequencePosition -- if it returns a nonempty list, then the given sublist is present:

(
set
 // Select[Length@SequencePosition[#, Alternatives @@ notallowed, 1] == 0 &]
 // Select@Function[list, 
       Length@SequencePosition[list, #, 1] > 0 & /@ allowed // Apply@And]
)
{{3, 4, 5, 6, 7, 9}, {3, 4, 5, 6, 11, 12}}
$\endgroup$
1
  • $\begingroup$ This is brilliant. Thank you. $\endgroup$ Commented Feb 20, 2018 at 2:34

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.