2
$\begingroup$

so, basically I have a list of sublists of varying length. For example,

list={{#,a,b,c,d},{#,e,f,g},{#,h,1},{#,2},{#},{#,k,l,m,n,o},{#,a,b,1,2},{1,2},{2,3},{3,4},{4,5}}

And I would like to end up with

listFiltered={{1,2},{2,3},{3,4},{4,5}}

So, what I would like to filter out is all the sublist containing any string. Now, instead of filtering for any sublist containing any string, I also know that each sublist that I want to filter out has the "#" character as its first sublist element.

This is then the repeating pattern, a "#" character as the first element of every sublist of arbitrary length, that contains a string somewhere, and therefore should be removed from the main list.

How should I go about achieving this? I have tried a number of things with DeleteCases and StringMatchQ but couldn't really get the syntax right. Thank you very much in advance!

$\endgroup$

2 Answers 2

4
$\begingroup$
Cases[{__Integer}] @ list

{{1, 2}, {2, 3}, {3, 4}, {4, 5}}

DeleteCases[{"#",___}] @ list

{{1, 2}, {2, 3}, {3, 4}, {4, 5}}

DeleteCases[{"#", ___}|{___,_Symbol,___}] @ list

{{1, 2}, {2, 3}, {3, 4}, {4, 5}}

Cases[Except[{"#" ,___}|{___,_Symbol,___}]] @ list

{{1, 2}, {2, 3}, {3, 4}, {4, 5}}

$\endgroup$
1
  • $\begingroup$ Thank you! This works! The only part I need to change is put "#" instead of just # since the characters in the lists are apparently "#". $\endgroup$ Oct 16, 2019 at 17:02
1
$\begingroup$
list = {{"#", a, b, c, d}, {"#", e, f, g}, {"#", h, 1}, {"#", 
    2}, {"#"}, {"#", k, l, m, n, o}, {"#", a, b, 1, 2}, {1, 2}, {2, 
    3}, {3, 4}, {4, 5}};

Pick[list, MemberQ[Except[__Integer]] /@ list, False]

Extract[list, Position[list, {__Integer}]]

SequenceReplace[list, k : {{___, Except[_Integer], ___}} :> Nothing]

Pick[list, NoneTrue[Head@# =!= Integer &] /@ list]

Pick[list, AllTrue[Head@# === Integer &] /@ list]

Result

{{1, 2}, {2, 3}, {3, 4}, {4, 5}}

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