LISTS: Extract every other element - SUMMARY

Charles Boncelet boncelet at udel.edu
Mon Dec 20 06:49:22 EST 1999


Randall Hopper wrote:
> 
>      Thanks for the suggestions.  Many of these are really novel ideas I
> hadn't thought of.

Here's one more (using mxTools):

>>>import NewBuiltins
>>>lst2=extract(lst,trange(0,len(lst),2))

On my machine, I do approach #1 10 times in 2.38 secs and the
mxTools approach in 0.65 secs.

I don't know why mxTools are not more widely used (ie. part of the
standard distribution).

--Charlie

> 
>      I coded each of these up, working with the same list of 100,000
> integers.  Here are the results.
> 
>      To my amazement, the simple for loop approach is pretty decent, as
> Mike mentioned.  Numeric will get you a little improvement (30-35%) if you
> use the shape-change column-selection approach Mike suggested.
> 
>   RESULTS:
> 
>                      Using range()                    Using xrange()
>                  -----------------------------------------------------------
>   APPROACH #1   |  100.00%  (2.13357 sec)    |     100.00%  (2.03318 sec)  *
>   APPROACH #1b  |   96.91%  (2.0677 sec)     |      98.20%  (1.9965 sec)   *
>   APPROACH #2   |  690.97%  (14.7423 sec)    |     719.24%  (14.6235 sec)  *
>   APPROACH #3   |  121.29%  (2.58789 sec)    |     122.02%  (2.48094 sec)  *
>   APPROACH #4   |  308.55%  (6.58309 sec)    |     323.81%  (6.58367 sec)
>   APPROACH #4b  |  705.80%  (15.0587 sec)    |     740.94%  (15.0646 sec)
>   APPROACH #5   |  104.58%  (2.23128 sec)    |     105.42%  (2.14335 sec)  *
>   APPROACH #6   |   66.42%  (1.41702 sec)    |      70.39%  (1.43119 sec)
> 
>                                   * = uses range/xrange
> 
>   -----------------------------------------------------------------------------
>   APPROACH KEY:
> 
>   APPROACH #1  - Original "for loop" implementation
>   APPROACH #1b - #1 but use multiply rather than divide
>   APPROACH #2  - filter( lambda a: a != None,
>                          map( even_select, lst, range(len(lst)) )
>   APPROACH #3  - map( lambda x: lst[x], range(0, len(lst), 2) )
>   APPROACH #4  - filter( on_off, lst ) , where on_off() funct alternates 1/0
>   APPROACH #4b - filter( onoff(), lst ), where onoff() class instance alt's 1/0
>   APPROACH #5  - a = Numeric.array( lst, 'O' )
>                  lst2 = list( Numeric.take( a, range(0,len(a),2) ) )
>   APPROACH #6  - data = Numeric.array( lst, Numeric.Int32 )
>                  data.shape = ( -1, step )
>                  lst2 = list( data[:,0] )
>   -----------------------------------------------------------------------------
> 
> --
> Randall Hopper
> aa8vb at yahoo.com

-- 
Charles Boncelet <boncelet at udel.edu>
University of Delaware
Newark DE 19716 USA
http://www.eecis.udel.edu/~boncelet/



More information about the Python-list mailing list