Re: position of a pattern in a list
- To: mathgroup at smc.vnet.net
- Subject: [mg58142] Re: [mg58138] position of a pattern in a list
- From: Andrzej Kozlowski <andrzej at akikoz.net>
- Date: Mon, 20 Jun 2005 05:21:18 -0400 (EDT)
- References: <200506190744.DAA05737@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On 19 Jun 2005, at 16:44, mark wrote: > *This message was transferred with a trial version of CommuniGate > (tm) Pro* > hi > i want to search for the positions > of the pattern "b" ~~ "c" > in the list: > lst = {{{"abcd", "cv"}, {"z", "zzbcz"}} > , {{"v"}, {"zbcx"}, {"xc", "bc"}}} > q = StringCases[#, "b" ~~ "c"] & /@ # & /@ lst > Out[]={{{{bc}, {}}, {{}, {bc}}}, {{{}}, {{bc}}, {{}, {bc}}}} > Position[q, {"bc"}] > Out[]={{1, 1, 1}, {1, 2, 2}, {2, 2, 1}, {2, 3, 2}} > this is okay. > but suppose i want to use position directly in the same way > i have used StringCases: > Position[#, "b" ~~ "c"] & /@ # & /@ lst > Out[]={{{}, {}}, {{}, {}, {{2}}}} > or > Position[lst, "b" ~~ "c"] > Out[]={{2, 3, 2}} > here the Position function could not detect the wanted pattern but > only the last explicit "bc", i hope you have ideas about this > behaviour. > my second question: can mathematica color "{" and "}" of the deep > nested list in a suitable way so the reader can pursue which one is > related to which. if not i think this is a valuable addition in any > future versions of mathematica. > thanks > > One possible way could be: DeleteCases[Cases[lst,x_String:>If[StringPosition[x,"bc"] != {},{ Position[lst,x],StringPosition[x,"bc"]}],Infinity],Null] {{{{1,1,1}},{{2,3}}},{{{1,2,2}},{{3,4}}},{{{2,2,1}},{{2,3}}},{{{ 2,3,2}},{{1,2}}}} Andrzej Kozlowski
- References:
- position of a pattern in a list
- From: mark <rotree2003@yahoo.com>
- position of a pattern in a list