Make me beautiful (code needs help)

Daniel Dittmar daniel.dittmar at sap.com
Tue Jul 23 11:48:40 EDT 2002


Daniel Dittmar wrote:
> Mark wrote:
>> for i in range(len(data["A"])):
>>      try:
>>           data["new"].append(data["A"][i] - data["A"][i-1])
>>      except OutOfBounds:
>>           data["new"].append("?")
>>
>
> from __future__ import generators
>
> def difference (collection):
>     iterator = iter (collection)
>     yield None
>     last = iterator.next ()
>     for current in iterator:
>         yield current - last
>         last = current
>
And a version which handles undefined entries, so you can now find the
differences of the differences:

def difference (collection):
    last = None
    for current in collection:
        try:
            yield current - last
        except TypeError:
            yield None
        last = current

a = [1,2,4,10,50]
>>>print list (difference (a))
[None, 1, 2, 6, 40]
>>>print list (difference (difference (a)))
[None, None, 1, 4, 34]

Daniel







More information about the Python-list mailing list