Re: A question about pattern-matching
- To: mathgroup at smc.vnet.net
- Subject: [mg63742] Re: [mg63728] A question about pattern-matching
- From: Bob Hanlon <hanlonr at cox.net>
- Date: Fri, 13 Jan 2006 04:48:19 -0500 (EST)
- Reply-to: hanlonr at cox.net
- Sender: owner-wri-mathgroup at wolfram.com
testList={ {-90,{{60493,0.061},{62493,0.15881}, {64493.5,0.559}}}, {-88,{{62493,0.151},{64493.,0.61617}, {65993.4,0.171},{68993.10,0.06}}}, {-86,{{62493.75,0.14},{64493.55,0.61440}, {65993.,0.18},{67993,0.0},{68993.100,0.06448}}}}; testList /. {_?NumericQ,x_List}:>Last/@x {{0.061,0.15881, 0.559},{0.151,0.61617,0.171,0.06},{0.14,0.6144,0.18,0.,0.06448}} testList /. {_?NumericQ,x:{{_,_}..}}:>Last/@x {{0.061,0.15881, 0.559},{0.151,0.61617,0.171,0.06},{0.14,0.6144,0.18,0.,0.06448}} Cases[testList,{_?NumericQ,x_List}:>Last/@x] {{0.061,0.15881, 0.559},{0.151,0.61617,0.171,0.06},{0.14,0.6144,0.18,0.,0.06448}} Cases[testList,{_?NumericQ,x:{{_,_}..}}:>Last/@x] {{0.061,0.15881, 0.559},{0.151,0.61617,0.171,0.06},{0.14,0.6144,0.18,0.,0.06448}} testList[[All,2]][[All,All,2]] {{0.061,0.15881, 0.559},{0.151,0.61617,0.171,0.06},{0.14,0.6144,0.18,0.,0.06448}} #[[2,All,2]]&/@testList {{0.061,0.15881, 0.559},{0.151,0.61617,0.171,0.06},{0.14,0.6144,0.18,0.,0.06448}} Last/@#&/@Last/@testList {{0.061,0.15881, 0.559},{0.151,0.61617,0.171,0.06},{0.14,0.6144,0.18,0.,0.06448}} Bob Hanlon > > From: gardyloo <gardyloo at mail.wsu.edu> To: mathgroup at smc.vnet.net > Subject: [mg63742] [mg63728] A question about pattern-matching > > Hi, all. > > I am trying to extract certain parts of a dataset, while retaining > some of the overall structure. I decided to do it using pattern-matching > and replacements, but have run into some problems. > > First, a small subset of the data: > > testList = {{-90, {{60493, 0.061}, {62493, 0.15881}, {64493.5, > > 0.559}}}, {-88, {{62493, 0.151}, {64493., 0.61617}, {65993.4, > > 0.171}, {68993.10, 0.06}}}, {-86, {{62493.75, 0.14}, {64493.55, > > 0.61440}, {65993., 0.18}, {67993, 0.0}, {68993.100, 0.06448}}}}; > > > The data consists of lists of the form { integer, { ordered pair1, > ordered pair2, ... } }, and my goal is to extract the second number in > each of the ordered pairs, while retaining the lists' overall structure. > So, for example, from testList, I'd want something like > { { 0.061, 0.15881, 0.559}, {0.151, 0.61617, 0.171, 0.06}, {0.14, > 0.61440, 0.18, 0.0, 0.06448}} > returned. > > I've tried several patterns, with variously named bits, and only some of > them make sense to me (and none of them, yet, return what I really > want). Can someone explain them to me? > > I'd expect this one to return the list of lists of ordered pairs. It > returns the full testList: > > testList /. {{(z_)?NumberQ, x:{{_Real, _Real}...}} -> x} > > > On the other hand, this one returns only the lists of ordered pairs: > > testList /. {{(z_)?NumberQ, x:{{_, _}...}} -> x} > > I thought that one was free to "type" the parts of the pattern to > return. I also thought that naming the parts rather freely was allowed. > The first three of the following return what I would expect, but the > last one, in naming the first of the ordered pairs' elements, returns > the testList apparently unchanged: > > In[54]:= > > testList /. {{z_, {x:{_, _}...}} -> z} > > testList /. {{z_, {x:{_, _}...}} -> x} > > testList /. {{z_, x:{{_, _}...}} -> x} > > testList /. {{z_, {x:{a_, _}...}} -> x} > > Out[54]= > > {-90, -88, -86} > > Out[55]= > > {{60493, 0.061}, {62493, 0.15881}, {64493.5, 0.559}, {62493, 0.151}, {64493., 0.61617}, > > {65993.4, 0.171}, {68993.1, 0.06}, {62493.75, 0.14}, {64493.55, 0.6144}, {65993., 0.18}, > > {67993, 0.}, {68993.1, 0.06448}} > > Out[56]= > > {{{60493, 0.061}, {62493, 0.15881}, {64493.5, 0.559}}, {{62493, 0.151}, {64493., 0.61617}, > > {65993.4, 0.171}, {68993.1, 0.06}}, {{62493.75, 0.14}, {64493.55, 0.6144}, {65993., 0.18}, > > {67993, 0.}, {68993.1, 0.06448}}} > > Out[57]= > > {{-90, {{60493, 0.061}, {62493, 0.15881}, {64493.5, 0.559}}}, > > {-88, {{62493, 0.151}, {64493., 0.61617}, {65993.4, 0.171}, {68993.1, 0.06}}}, > > {-86, {{62493.75, 0.14}, {64493.55, 0.6144}, {65993., 0.18}, {67993, 0.}, > > {68993.1, 0.06448}}}} > > > Although a solution would help me a lot, I really would like to know > what I'm missing in applying these replacement rules. > > Thanks! > Curtis O. > >