Make me beautiful (code needs help)

Fernando Perez fperez528 at yahoo.com
Tue Jul 23 12:02:49 EDT 2002


Mark wrote:

> Ok, I have the following data:
> 
> data["A"] = [1,2,4,10,50]
> 
> and I want to get:
> 
> data["new"] = [?, 1, 2, 6, 40]
> 
> That is, I want to get the successive differences into their proper place.
> I don't care what is in the question mark spot.  I'll figure that out later.

Use Numeric:

In [9]: from Numeric import *

In [10]: data ={}

In [11]: data["A"] = array([1,2,4,10,50])

In [12]: data["new"] = data['A'][1:] - data['A'][:-1]

In [13]: data['new']
Out[13]: array([ 1,  2,  6, 40])


> Now, the other problem, is that in general, I want to fill data["new"] with
> an arbitary function of the values in data["A"].  As a dumb example, I
> might want to fill data["new"] with copies of the mean (average) of
> data["A"].  So, in some cases, the new values are relative to the current
> location (ie i and i-1) and in other they are absolute (ie all of them).
> 
> I can certainly hack through this, but I'm looking for something pretty.  I
> know python is up to it.

For this kind of thing, look at Numeric. It has a ton of functions ready-made 
for numerical processing, very convenient slicing syntax (better than python 
lists) and as a bonus it's fast. Explicit loops in python over large lists 
for numerical processing take an eternity and a half. That's what Numeric is 
for.

cheers,

f.



More information about the Python-list mailing list