1
$\begingroup$

I've a table with 3 columns. The table looks like this:

test = {
  {"john", {"suzan", "jan"}, {"new", "job", "policies"}}, 
  {"carla", {"john", "piet"}, {"route", "suggestion"}}, 
  {"jan", {"john", "suzan"}, {"re", "new", "job", "policies"}}, 
  {"john", {"piet", "carla"}, {"re", "route", "suggestion"}}}

I would like to select the records with has no re in the third column. When I select the third column like this:

test1 = Select[test[[All, 3]], #[[1]] != "re" &]

The result is:

{{"new", "job", "policies"}, {"route", "suggestion"}}

But I want to select also the columns one and two. When I write:

Select[test[[All, {1, 2, 3}]], #[[3]] != "re" &]

The result is empty

{}

Who has a suggestion to solve this?

$\endgroup$
2
$\begingroup$

Just some other ways:

Cases[test, Except[{_, _, {___, "re", ___}}]]

or

Pick[test, FreeQ[#[[3]], "re"] & /@ test]
$\endgroup$
2
$\begingroup$

I think Select is the right method.

Select[test, !MemberQ[#[[3]], "re"] &]

Output:

{{"john", {"suzan", "jan"}, {"new", "job", "policies"}},
   {"carla", {"john", "piet"}, {"route", "suggestion"}}}

Which equals line 1 and 2. The MemberQ functions tests the third column for strings containing re.

$\endgroup$
  • $\begingroup$ You can also use FreeQ[#[[3]],"re"] for the test function. (+1) $\endgroup$ – kglr Apr 13 '14 at 2:10

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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