0
|
4700 Views
|
5 Replies
|
7 Total Likes
View groups...
Share
GROUPS:

Selecting strings by numeric character values

Posted 11 years ago
 "{190012}", "{90013}", "{80016}", "{90022}", "{90034}", "{90042}", \"{90102}", "{90106}", "{90115}", "{50131}", "{90132}", "{90137}", \"{90153}", "{90214}", "{90222}", "{90226}", "{90230}", "{90234}", \"{90239}", "{90247}", "{90304}", "{90306}", "{70312}", "{90316}", \"{90323}", "{90325}", "{90328}", "{90331}", "{90332}", "{90333}",How to select  numbers that begin with specified digit for example 8 ?These numbers are not possessed of having attribute of numbers yet.They are strings .
5 Replies
Sort By:
Posted 11 years ago
 Here are 3 another options, with time test: list={"{190012}","{90013}","{80016}","{90022}","{90034}","{90042}","{90102}","{90106}","{90115}","{50131}","{90132}","{90137}","{90153}","{90214}","{90222}","{90226}","{90230}","{90234}","{90239}","{90247}","{90304}","{90306}","{70312}","{90316}" }; listBig=Join@@ConstantArray[list,{100000}];  Select[listBig,StringMatchQ[#,"{8*}"]&];//AbsoluteTiming//First Cases[listBig,x_/;StringMatchQ[x,"{8*}"]];//AbsoluteTiming//First Extract[listBig,Position[listBig,x_/;StringMatchQ[ToString@x,"{8*}"],{1}]];//AbsoluteTiming//First  1.493229 1.4565284.747536
Posted 11 years ago
 StringCases use of StringPattern can also pull out matches with sub-stringsStringCases[yourlst, "{8" ~~ __]{{}, {}, {"{80016}"}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}}StringCases[yourlst, "{" ~~ num : ("8" ~~ __) ~~ "}" :> num]{{}, {}, {"80016"}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}}Flatten to get rid of empty sub-string resultsFlatten@StringCases[yourlst, "{" ~~ num : ("8" ~~ __) ~~ "}" :> num]{"80016"}
Posted 11 years ago
 An other approach is to use Pick and StringMatchQ. Say we have this.j = {"190012", "90013", "80016", "90022", "50131", "865543","78880999", "8003003"}Then we can use thisPick[j, StringMatchQ[j, "8" ~~ ___ ~~ ___]]with this output{"80016", "865543", "8003003"}Paul.
Posted 11 years ago
 An alternative approach:Select[{"{1234}", "{4321}"}, Characters[#][[2]] == "4" &]A further possibility would be to thrown in a ToExpression on Characters, if the comparison needs to be numeric.
Posted 11 years ago
 Hey Marek, The following code will do what you want. Now I have a list of number and I want to take out the string that contains a number begins with 4. Cases[{"{1234}", "{4321}"}, x_ /; (IntegerDigits@ToExpression[x][[1]])[[1]] == 4 ]The main body is Case[ , pattern]. For our default pattern "x_", it matches everything so I need to add a condition here ("/;"), which is "the first digit be 4" .In the condition, I convert the element into a computable expression (ToExpression["{123}"] -> {123} = List[123]). Then I take the first element from the list, from last example, yields an integer, 123. IntegerDigits breaks this number into {1,2,3}. Finally, I check if the first element  ( {1,2,3} [[1]] -> 1) matches 4.