Re: Conditional drop of list elements
- To: mathgroup at smc.vnet.net
- Subject: [mg63591] Re: Conditional drop of list elements
- From: Paul <gleam at flashmail.com>
- Date: Fri, 6 Jan 2006 05:24:37 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
> Hi guys, > > I need your assistance by the following problem: > > list1 = {{0,A,A,A}, {1,B,B,B}, {0,C,C,C}, {1,D,D,D}, > {1,F,F,F}, > {0,G,G,G}, {0,H,H,H}} > > Every 1st element of the each sublist should be > checked. Is the first > element a ZERO, this sublist should be removed from > the list. If it is > a ONE - this sublist remains in the list. > > In this example the output should be: > > {{1,B,B,B}, {1,D,D,D},{1,F,F,F}} > > Thanks for your help! > > LZ Hello LectorZ, I believe that Pick is the better function for this application, if you are using Mathematica version 5.1 or later. Please consider: In[1]:= list1=Table[Random[Integer], {250000}, {5}]; In[2]:= AbsoluteTiming[Pick[list1, list1[[All, 1]], 1];] Out[2]= {0.1875000 Second, Null} In[3]:= AbsoluteTiming[Cases[list1, {1, ___}];] Out[3]= {0.3437500 Second, Null} In[4]:= AbsoluteTiming[DeleteCases[list1, {0, ___}];] Out[4]= {0.4218750 Second, Null} In[5]:= AbsoluteTiming[list1 /. {0, ___} :> Sequence[];] Out[5]= {0.5000000 Second, Null} In[6]:= AbsoluteTiming[Select[list1, #[[1]] == 1 &];] Out[6]= {0.8906250 Second, Null} Paul
- Follow-Ups:
- Re: Re: Conditional drop of list elements
- From: "Carl K. Woll" <carlw@wolfram.com>
- Re: Re: Conditional drop of list elements