1

Let's say I have an array:

A = [20, 18, 25, 33, 32, 22, 14, 20, 24, 33, 66, 70, 60, 50]

How do I get an array B that is the first minus the second, the second minus the third so on to get this:

B = [2, -7, -8, 1, 10, 8, -6, -4, -9, -33, -4, 10, 10]

and then plot B?

Thank you for your help!

I have tried to do:

B = ([(n+1)-n] for n in zip(A)) 

but when I try to plot the graph it gives back the error:

matplotlib does not support generators as input

Also to explain better the array is much longer and in decimals, this is just an approximation of what the actual problem is, thank you!

Patrik
  • 499
  • 1
  • 7
  • 24
AAAA
  • 23
  • 3

1 Answers1

2

You can fix your code with:

B = [a-b for a,b in zip(A, A[1:])]

On the other note, with this kind of operations, you should try with numpy, for example: np.diff:

import numpy as np
B = np.diff(A[::-1])[::-1]

or, similar to your idea:

A = np.array(A)

B = A[:-1] - A[1:]
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74