4
$\begingroup$

I have a string list that consists of elements that have 3 sub-elements:

lis = {{"ac", "b", "ca"}, {"ab", "cde", "f,h"}, {"cd", "gh", "kl,"}};

I would like to delete each list member whose third element lacks a comma, which gives:

res = {{"ab", "cde", "f,h"}, {"cd", "gh", "kl,"}}

Thanks for any advice.

$\endgroup$

3 Answers 3

7
$\begingroup$
Select[lis,StringContainsQ[","]@*Last]
$\endgroup$
6
$\begingroup$
Cases[lis, {__, _?(StringContainsQ[","])}]

alternatively

Replace[lis, x_ /; StringFreeQ[Last[x], ","] -> Nothing, {1}]
$\endgroup$
4
$\begingroup$

Selecting sublist elements where the last element has a comma:

Pick[lis, StringContainsQ[Last /@ lis, ","]]

Selecting sublist elements where the last element lacks a comma:

Pick[lis, StringFreeQ[Last /@ lis, ","]]
$\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.