Re: Pattern to match element in nested list
- To: mathgroup at smc.vnet.net
- Subject: [mg122845] Re: Pattern to match element in nested list
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Sat, 12 Nov 2011 07:34:12 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
On 11/11/11 at 4:54 AM, ebaughjason at gmail.com (JasonE) wrote: >What pattern would pick students with a score of zero out of this >list (which is {"student", score}): >{{"Smith, Tim",3},{"Wheres,Waldo",4},{"Muffet, LilMiss",0},{"Brown, >Charlie",1},{"X, Doctor",0}} A couple of simple ways to get the desired result: In[8]:= data = {{"Smith, Tim", 3}, {"Wheres,Waldo", 4}, {"Muffet, LilMiss", 0}, {"Brown, Charlie", 1}, {"X, Doctor", 0}}; In[9]:= Cases[data, {__, 0}] Out[9]= {{"Muffet, LilMiss", 0}, {"X, Doctor", 0}} In[10]:= Select[data, Last@# == 0 &] Out[10]= {{"Muffet, LilMiss", 0}, {"X, Doctor", 0}} or if you wanted to do the same task via pattern matching: In[11]:= data /. {__, _?Positive} :> Sequence[] Out[11]= {{"Muffet, LilMiss", 0}, {"X, Doctor", 0}}