0

I would like to know how to find the nth term - (n-1)th term within a list. I know it should be very simple, but I'm just having trouble with application. So given the input of [0, 2, 4, 6, 5], the function should return [0, 2, 2, 2, -1]. My solution (which doesn't work) is as follows:

def cost_to_diff (stocks):
    for i in range(i+1):
        stock[i] = stock[i] - stock[i-1]
        print stock

Why doesn't this work as intended?

Andrew Li
  • 55,805
  • 14
  • 125
  • 143
  • possible duplicate - https://stackoverflow.com/questions/24614361/finding-the-difference-between-consecutive-numbers-in-a-list-python#24742315 – Ganesh Tata Dec 13 '17 at 07:41
  • How did you think `i in range(i+1)` would work? A value cannot be used and defined in the same line like that – OneCricketeer Dec 13 '17 at 07:42
  • 2
    Possible duplicate of [Finding the difference between consecutive numbers in a list (Python)](https://stackoverflow.com/questions/24614361/finding-the-difference-between-consecutive-numbers-in-a-list-python) – styvane Dec 13 '17 at 07:43
  • how do you define your function when `n = 0`? – Xero Smith Dec 13 '17 at 08:03

3 Answers3

2

Your mistake is that you reset the ith element of list, so on the next cycle iteration you access it's changed value.

This will print what you need

def cost_to_diff (stocks):
    for i in range(1, len(stocks)):
        print stocks[i] - stocks[i-1]

If you want to print a list, then do

def cost_to_diff (stocks):
    diff = []
    for i in range(1, len(stocks)):
        diff.append(stocks[i] - stocks[i-1])
    print diff

or

def cost_to_diff (stocks):
    print [stocks[i] - stocks[i-1] for i in range(1, len(stocks))]

Also I advice you to try numpy for such tasks. This function will do the job. Especially if you are playing with stock data, numpy will be much faster than lists. Moreover for stock data one usually uses data frames, so pandas is what you need to study, and Series.diff is the function you need.

Leonid
  • 1,127
  • 2
  • 15
  • 29
0

You are using the wrong range. You need to find out the length of the stocks list, and then build your range accordingly.

differences = []
for i in range(len(stocks) - 1):
    differences.append(stock[i+1] - stock[i])
    print differences

However, there is a better way to do this. Python has several builtins to help you do this kind of stuff easily. Use the zip function to get your elements without messing around with indexes.

differences = []
for a, b in zip(l, l[:1]):
    differences.append(b-a)

For example:

my_list = [0, 3, 4, 5, 7]
print zip(my_list, my_list[1:])

This produces output

[(0, 3), (3, 4), (4, 5), (5, 7)]

All you need to do after is subtract the elements.

0

The following code returns [2, 2, 2, -1] because I don't get why you will get 0 for the first element.

Do you assume that the first -1 element is also 0?

len('list') will give you the length of list.

range(start, end) will give you a range.

Since you like to start your iterations in the for loop at the second element (index = 1) start will be 1 and end will be the length of the stock list.

stock = [0,2,4,6,5] 
result = []#create an empty list for the results
for i in range(1,len(stock)):#set the iteration steps, will be 1,2,3,4 in this case
    result.append(stock[i] - stock[i-1])#By using stack[i] = ... you will overwrite the ith element
print result
jwpfox
  • 5,124
  • 11
  • 45
  • 42
Sebus
  • 55
  • 1
  • 7