MathGroup Archive 2006

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

Search the Archive

Re: Re: Conditional drop of list elements

  • To: mathgroup at smc.vnet.net
  • Subject: [mg63605] Re: [mg63591] Re: Conditional drop of list elements
  • From: "Carl K. Woll" <carlw at wolfram.com>
  • Date: Sat, 7 Jan 2006 02:29:28 -0500 (EST)
  • References: <200601061024.FAA14629@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Paul wrote:
>>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

The fastest approach for selecting a conditional subset of a large data 
set usually works as follows:

1. Construct a list of 0's and 1's corresponding to which elements you 
want to keep.

2. Extract the position of the 0's or 1's.

3. Use Part to obtain the subset.

Here is a function that does the above:

getsublist[data_] := Module[{mask, pos},
   mask = Sign[Abs[data[[All, 1]] - 1]];
   pos = SparseArray[mask, Automatic, 1] /.
     SparseArray[_, _, _, p_] :> Flatten[p[[2, 2]]];
   data[[pos]]]

Some sample data:

In[1]:=
list1=Table[Random[Integer],{10^6},{5}];

And a comparison:

In[3]:=
r1=Pick[list1,list1[[All,1]],1];//Timing
r2=getsublist[list1];//Timing
r1==r2

Out[3]=
{1.016 Second,Null}

Out[4]=
{0.187 Second,Null}

Out[5]=
True

So, getsublist is over 5 times faster.

Carl Woll
Wolfram Research


  • Prev by Date: Re: Re: Types in Mathematica, a practical example
  • Next by Date: Another question: How to add some assumptions to this Integrand?
  • Previous by thread: Re: Conditional drop of list elements
  • Next by thread: Re: Re: Conditional drop of list elements