Make me beautiful (code needs help)

Alex Martelli aleax at aleax.it
Wed Jul 24 04:16:10 EDT 2002


Emile van Sebille wrote:

> Mark
>> Ok, I have the following data:
>> 
>> data["A"] = [1,2,4,10,50]
>> 
>> and I want to get:
>> 
>> data["new"] = [?, 1, 2, 6, 40]
> 
> 
>>>> A = [1,2,4,10,50]
>>>> ['?']+[a-b for b,a in zip(A,A[1:])]
> ['?', 1, 2, 6, 40]

Fine (and most elegant) for relatively small sequences, where you
don't mind zipping the sequence with a shifted copy of itself.

Otherwise, the normal way to do it is the less-elegant:

['?'] + [ A[i+1]-A[i] for i in range(len(A)-1) ]


Alex




More information about the Python-list mailing list