4

I have the following dataset

data = {{"one", "two", "three", "a", "b", "c"}, {"four", "five", "d"}}

I want to delete all the strings with length < 2. The desired output is:

{{"one", "two", "three"}, {"four", "five"}}

I found several solutions on this site like:

DeleteCases[#, x_ /; Length[x] < 2] & /@ data

But none of them resulted in my desired output. Any suggestion?

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
Michiel van Mens
  • 2,835
  • 12
  • 23

5 Answers5

9

many ways to do that..

DeleteCases[#, s_String /; StringLength[s] < 2] & /@ data
DeleteCases[data, s_String /; StringLength[s] < 2, {2}]
Select[#, StringLength[#] > 1 &] & /@ data
data /. s_String /; StringLength[s] < 2 :> Sequence @@ {}
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
george2079
  • 38,913
  • 1
  • 43
  • 110
3
Table[Select[data[[i]], StringLength@# >= 2 &], {i, Length@data}]
ZaMoC
  • 6,697
  • 11
  • 31
3
Pick[#, UnitStep[StringLength@# - #2], 1] &[data, 2] (*thanks: @AlexeyPopkov*)

{{"one", "two", "three"}, {"four", "five"}}

kglr
  • 394,356
  • 18
  • 477
  • 896
1
data = {{"one", "two", "three", "a", "b", "c"}, {"four", "five", "d"}}
data // Map[StringLength@# >= 2 &, #, {-1}] & // Pick[data, #] & (*{2}->{-1}*)
webcpu
  • 3,182
  • 12
  • 17
  • 1
    With third argument of Map being {-1} instead of {2} your solution generalizes to arbitrary deeply nested lists of strings. – Alexey Popkov Jun 01 '17 at 17:54
0
Map[Cases[__?(StringLength[#] >= 2 &)]][data]
Map[Cases[x__/;StringLength[x] >= 2]][data]
Ali Hashmi
  • 8,950
  • 4
  • 22
  • 42