Message Boards Message Boards

0
|
4553 Views
|
5 Replies
|
7 Total Likes
View groups...
Share
Share this post:

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 .
POSTED BY: Artur Kotarski
5 Replies
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.456528
4.747536
POSTED BY: Rodrigo Murta
StringCases use of StringPattern can also pull out matches with sub-strings
StringCases[yourlst, "{8" ~~ __]
{{}, {}, {"{80016}"}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}}

StringCases[yourlst, "{" ~~ num : ("8" ~~ __) ~~ "}" :> num]
{{}, {}, {"80016"}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}}

Flatten to get rid of empty sub-string results
Flatten@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 this
Pick[j, StringMatchQ[j, "8" ~~ ___ ~~ ___]]

with this output
{"80016", "865543", "8003003"}

Paul.
POSTED BY: Paul Cleary
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 BY: Peter Fleck
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[ <my list>, 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.  
POSTED BY: Shenghui Yang
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract