7

I have a list:

lis = {{"abc", 1, 2}, {"cde", 3, 4}, {"fgbc", 5, 6}}

I would like to select list items that have first elements that end in the string "bc" to give:

res = {{1,2},{5,6}}

This is a variant on this question which has to do with strings only. Thank you as always for suggestions.

Suite401
  • 4,793
  • 8
  • 18

7 Answers7

9
Pick[lis[[All, 2 ;;]], StringEndsQ[lis[[All, 1]], "bc"]]

{{1, 2}, {5, 6}}

Also

f[{_String?(StringEndsQ["bc"]), x__}] := {x}
f[_] := Sequence[]
f /@ lis

{{1, 2}, {5, 6}}

kglr
  • 394,356
  • 18
  • 477
  • 896
7
lis = {{"abc", 1, 2}, {"cde", 3, 4}, {"fgbc", 5, 6}};

Using Cases

Cases[lis, {_?(StringTake[#, -2] == "bc" &), x_, y_} :> {x, y}, 
  Infinity]

(* {{1, 2}, {5, 6}} *)

or

Rest /@ Cases[lis, {_?(StringTake[#, -2] == "bc" &), __}, Infinity]

(* {{1, 2}, {5, 6}} *)

Or using Select

Rest /@ Select[lis, StringTake[#[[1]], -2] == "bc" &]

(* {{1, 2}, {5, 6}} *)

Or using DeleteCases

Rest /@ DeleteCases[lis, {_?(StringTake[#, -2] != "bc" &), __}, 
  Infinity]

(* {{1, 2}, {5, 6}} *)
Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198
4

This uses string matching on the first element of each sublist and then discards the first element from the matches.

lis = {{"abc", 1, 2}, {"cde", 3, 4}, {"fgbc", 5, 6}};
Rest/@Select[lis,StringMatchQ[First[#],RegularExpression["\\w*bc"]]&]

(* {{1, 2}, {5, 6}} *)

You may need to adjust the pattern given to RegularExpression if your strings do not all begin with word characters.

Bill
  • 12,001
  • 12
  • 13
3

This works too:

If[StringEndsQ[#1, "bc"], {##2}, Nothing] & @@ # & /@ lis

since list elements that are Nothing are automatically stripped from the sequence.

2
lis = {{"abc", 1, 2}, {"cde", 3, 4}, {"fgbc", 5, 6}};

Using SubsetMap:

SubsetMap[StringEndsQ[#, "bc"] &, lis, {All, 1}] // 
  Select[ContainsNone[{False}]] // Map[Rest]

Using Sow/Reap:

Scan[If[StringEndsQ[First@#, "bc"], Sow[Rest@#], Nothing] &, lis] // 
   Reap // Last // First

Result

{{1, 2}, {5, 6}}

Syed
  • 52,495
  • 4
  • 30
  • 85
2
lis = {{"abc", 1, 2}, {"cde", 3, 4}, {"fgbc", 5, 6}};

Values @ KeySelect[StringEndsQ @ "bc"] @ MapApply[# -> {##2} &] @ lis

{{1, 2}, {5, 6}}

eldo
  • 67,911
  • 5
  • 60
  • 168
2

Using Cases and StringEndsQ:

Cases[lis, {y_ /; StringEndsQ[y, "bc"], x__} :> {x}]

({{1, 2}, {5, 6}})

E. Chan-López
  • 23,117
  • 3
  • 21
  • 44