Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2011

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

Search the Archive

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}}




  • Prev by Date: Re: Domain of a function
  • Next by Date: Re: large integration result for simple problem: 1/x,, also BesselJ
  • Previous by thread: Re: Pattern to match element in nested list
  • Next by thread: Re: Pattern to match element in nested list