Make me beautiful (code needs help)

Tom Good Tom_Good1 at excite.com
Tue Jul 23 17:28:08 EDT 2002


Mark <Hobbes2176 at yahoo.com> wrote in message news:<HVd%8.6582$I4.1825 at nwrddc03.gnilink.net>...
> Ok, I have the following data:
> 
> data["A"] = [1,2,4,10,50]
> 
> and I want to get:
> 
> data["new"] = [?, 1, 2, 6, 40]
> 

Maybe this is close to what you want:

from __future__ import generators

def processPairs(x, func):
    for i in range(len(x)-1):
        yield(func(x[i], x[i+1]))

def difference(x, y):
    return y-x 

if __name__ == '__main__':
    test = [1, 2, 4, 10, 50]
    print [i for i in processPairs(test, difference)]


> Now, the other problem, is that in general, I want to fill data["new"] with 
> an arbitary function of the values in data["A"].

Create any function taking two arguments and call it instead of
difference().  Hope this helps.  (This code will not put the "?" in as
the first element, but you could add that in if it is important.)


Tom



More information about the Python-list mailing list