4
$\begingroup$

This query is similar to a question I posted earlier.

Given

testList = 
  {{"a", "b 3", "cd", "ef 23"}, {"z 12", "y", "x"}, {"z", "y"}, {"a", "b3"}};

I would like to create a rule that would delete 1st level elements of testList that do not contain a string representation of a number. The result would be:

resultList = {{"a", "b 3", "cd", "ef 23"}, {"z 12", "y", "x"}, {"a", "b3"}};
$\endgroup$

3 Answers 3

6
$\begingroup$

Although I very much in favor of aardvark2012's answer using Pick, I would like to note that this can done with good old Table.

data = {{"a", "b 3", "cd", "ef 23"}, {"z 12", "y", "x"}, {"z", "y"}, {"a", "b3"}};
Table[If[Or @@ StringContainsQ[item, DigitCharacter], item, Nothing], {item, data}]

{{"a", "b 3", "cd", "ef 23"}, {"z 12", "y", "x"}, {"a", "b3"}}

$\endgroup$
8
$\begingroup$

One possibility:

Pick[#, Or @@@ StringContainsQ[DigitCharacter] /@ #] &@testList2

or

Select[testList2, Or @@ StringContainsQ[#, DigitCharacter] &]
$\endgroup$
2
$\begingroup$

If the OP really wants a Rule then anothe options is:

rule = Rule[
  PatternTest[Blank[List],
   Composition[
    Not,
    Apply[Or],
    Map[StringContainsQ[DigitCharacter]]
    ]
   ]
  , Nothing
  ]

Or

rule= _List?(Not@*Apply[Or]@*Map[StringContainsQ[DigitCharacter]]) -> Nothing 

Then

testList /. rule
(* {{"a", "b 3", "cd", "ef 23"}, {"z 12", "y", "x"}, {"a", "b3"}} *)

(testList /. rule) == resultList
(* True *)
$\endgroup$
1
  • $\begingroup$ Although admonished not to say thanks, I'll say thanks /@ aardvark2012, m_goldberg,rhermans :) $\endgroup$
    – Suite401
    Sep 24, 2017 at 5:18

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.