Skipping duplicate values ​​when reading csv in Python

I am trying to subtract the previous item in a list from the next item in the list, but I think my type is preventing me from doing so. The type of each item in the list is int. If I have a list of integers like

1 2 3 4 5 6 7

      

How to subtract 1 from 2, 2 from 3, 3 from 4, etc. and print this value after every operation?

My list is a torcount that I acquired from a numpy operation and this is the code I tried:

TorCount=len(np.unique(TorNum))
for i in range(TorCount):
    TorCount=TorCount[i]-TorCount[i-1]
    print TorCount

      

thank

+3


source to share


1 answer


Use np.diff

:

Example:



>>> xs = np.array([1, 2, 3, 4])
>>> np.diff(xs, n=1)
array([1, 1, 1])

      

numpy.diff(a, n=1, axis=-1)

Calculate the nth order discrete difference along the given axis.

The first order difference is defined by the expression [n] = a [n + 1] - a [n] along the given axis, the higher order differences are calculated using diff recursively.

+5


source







All Articles