MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: position of a pattern in a list


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


  • Prev by Date: Re: Nested Commutators
  • Next by Date: Re: Nested Commutators
  • Previous by thread: position of a pattern in a list
  • Next by thread: Re: position of a pattern in a list