Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #6399
| From | Bill Rowe <readnews@sbcglobal.net> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: Pattern to match element in nested list |
| Date | 2011-11-12 12:36 +0000 |
| Organization | Steven M. Christensen and Associates, Inc and MathTensor, Inc. |
| Message-ID | <j9lp8v$lfc$1@smc.vnet.net> (permalink) |
On 11/11/11 at 4:54 AM, ebaughjason@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}}
Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread
Re: Pattern to match element in nested list Bill Rowe <readnews@sbcglobal.net> - 2011-11-12 12:36 +0000
csiph-web