fast sub list operations

Chris Barker chrishbarker at home.net
Tue Oct 16 14:48:19 EDT 2001


Robin Becker wrote:

> I know about NumPy and like it a lot, but it's a bit heavyweight for
> what we need. 

NumPy really is what you need. It may be able to do more than you think
you need at the moment, but if you start using it, you will probably
find that you are using more and more of its functionality every day.
And what is the downside???

>From your original post:

Robin Becker wrote:
> I constantly miss some nice way to subset lists.

This is the take() function of Numpy

> have a list of x y coordinates eg
> 
>         [x0,y0,x1,y1,.....]

This is begging to be represented as a NX2 array (or at least a list of
tuples...)

>>> from Numeric import *
>>> coords = [1,2,3,4,5,6,7,8]
>>> A = array(coords)
>>> A.shape = (4,2)
>>> A
array([[1, 2],
       [3, 4],
       [5, 6],
       [7, 8]])
 
> and wish to perform the operation x->x+v, y->y+w for the co-ordinates in
> the list I don't seem to be able to do this fast using map.

Now you want to do a fast math operation on a sequence of numbers...

>>> v,w = 3,4
>>> A = A * (v,w)
>>> A
array([[ 3,  8],
       [ 9, 16],
       [15, 24],
       [21, 32]])

> had a way to slice the list nicely into
> 
> X=[x0,x1,.....x(n-1)] & Y=[y0,y1,.....,y(n-1)]

>>> X,Y = A[:,0],A[:,1]
>>> X
array([ 3,  9, 15, 21])
>>> Y
array([ 8, 16, 24, 32])
 
I don't seem to have an interlace to get back to the original
> list format.

>>> list(A.flat)
[3, 8, 9, 16, 15, 24, 21, 32]

> How can these kinds of operations be performed quickly in current python

you got some suggestions, but none of them are nearly as clean or fast
as Numeric.

> and what if any new features would python require to do them best?

An array-oriented math package......Oh! that's what NumPy is!!!!!

Why not use the right tool for the job?

-Chris

-- 
Christopher Barker,
Ph.D.                                                           
ChrisHBarker at home.net                 ---           ---           ---
http://members.home.net/barkerlohmann ---@@       -----@@       -----@@
                                   ------@@@     ------@@@     ------@@@
Oil Spill Modeling                ------   @    ------   @   ------   @
Water Resources Engineering       -------      ---------     --------    
Coastal and Fluvial Hydrodynamics --------------------------------------
------------------------------------------------------------------------



More information about the Python-list mailing list