1

here is my code.

A = [86.14803712, 85.25496701, 86.50334271, 86.0266668,  86.61455594, 86.90445213, 86.65519315, 87.10116762, 87.08173861]
B = []
i = 0
for i in range(len(A)):
    c = A[i]-A[i-1]
    B.append(c)
    print(c)

I want to get the differences between two continuous numbers in this list, eg,(85.25496701-86.14803712). So in the results, I should have eight numbers as results.

But the results I get are:

-0.9337014900000042
-0.8930701099999965 
1.2483756999999969
-0.4766759099999973
0.5878891400000015
0.2898961899999932
-0.24925897999999336
0.4459744699999959
-0.019429009999996083

I don't need -0.9337014900000042 since it comes from the first number subtract the last number in the list. What should I do the fix it? Thanks

Ma0
  • 15,057
  • 4
  • 35
  • 65
Ben Wang
  • 99
  • 3
  • 10

6 Answers6

1

That's the strength and the weakness of python: index -1 is always valid when the list isn't empty, which can lead to programs not crashing but not doing what you want.

For those operations, it's better to use zip to interleave the list with a sliced version of itself without the first number:

A = [86.14803712, 85.25496701, 86.50334271, 86.0266668,  86.61455594, 86.90445213, 86.65519315, 87.10116762, 87.08173861]

diffs = [ac-ap for ac,ap in zip(A[1:],A)]

or with itertools.islice to avoid creating a new list to iterate on it:

import itertools
diffs = [ac-ap for ac,ap in zip(itertools.islice(A,1,None),A)]

result (8 values):

[-0.8930701099999965, 1.2483756999999969, -0.4766759099999973, 0.5878891400000015, 0.2898961899999932, -0.24925897999999336, 0.4459744699999959, -0.019429009999996083]
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
0

It's possible to do this in base Python, but you might like the semantic clarity of Pandas:

import pandas as pd
pd.Series(A).diff().values[1:]

array([-0.89307011,  1.2483757 , -0.47667591,  0.58788914,  0.28989619,
       -0.24925898,  0.44597447, -0.01942901])
andrew_reece
  • 20,390
  • 3
  • 33
  • 58
0

You can just do:

B = [x-y for x, y in zip(A[1:], A)]
print(B)  # -> [-0.8930701099999965, 1.2483756999999969, -0.4766759099999973, 0.5878891400000015, 0.2898961899999932, -0.24925897999999336, 0.4459744699999959, -0.019429009999996083]
Ma0
  • 15,057
  • 4
  • 35
  • 65
0

You need to make sure you star from a correct index. In your current code, in the first iteration of the loop you will be computing A[0] - A[-1]. Hence, you need to start i from 1 to ensure in the first iteration you compute the value of A[1] - A[0]. The corrected version of your code is here:

A = [86.14803712, 85.25496701, 86.50334271, 86.0266668,  86.61455594, 86.90445213, 86.65519315, 87.10116762, 87.08173861]
B = []
i = 0
for i in range(1, len(A)):
    c = A[i]-A[i-1]
    B.append(c)
    print(c)
MTMD
  • 1,162
  • 2
  • 11
  • 23
0

I think the problem is that the loop subtracts the first element to the last because the loop starts at index 0 and subtracts it from index -1 (python takes -1 as the last index of a list). A better solution imo would be:

A = [86.14803712, 85.25496701, 86.50334271, 86.0266668,  86.61455594, 
86.90445213, 86.65519315, 87.10116762, 87.08173861]
B = []
i = 0
for i in range(len(A)-1):
    c = -(A[i]-A[i+1])
    B.append(c)
    print(c)
Abby
  • 306
  • 3
  • 13
0

The easiest would be:

result = [x-y for x,y in zip(A[1:], A[:-1])]
Matan Hugi
  • 1,110
  • 8
  • 16