Make me beautiful (code needs help)

Daniel Fackrell dfackrell at DELETETHIS.linuxmail.org
Tue Jul 23 11:33:55 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]
>
> 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.
>
> Here's my current code:
>
> for i in range(len(data["A"])):
>      try:
>           data["new"].append(data["A"][i] - data["A"][i-1])
>      except OutOfBounds:
>           data["new"].append("?")
>
> 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.
>
> Regards,
> Mark

How about this for the narrow case?  If you really want the new list to be
the same length as the original one, you can pad at the start or end:

A= [1, 2, 4, 10, 50]
new = []
for i in range(len(A)-1):
    new.append(A[i+1] - A[i])

--
Daniel Fackrell (dfackrell at linuxmail.org)
When we attempt the impossible, we can experience true growth.





More information about the Python-list mailing list