An example of what I want to do:
What I want to do is have the first element equal itself, and every other equals itself minus the previous.
array = [12, 16, 31, 53, 70]
new = []
for x in range(5):
for value in array:
value -= array[x]
new.append(value)
#print new
#should be new = [12, 4, 15, 22, 17]
I'm running it in my interpreter and getting this:
[0, 4, 19, 41, 58, -4, 0, 15, 37, 54, -19, -15, 0, 22, 39, -41, -37, -22, 0, 17,
-58, -54, -39, -17, 0]
Is my code way off, any suggestions to get it working?
2006-07-05
16:30:15
·
2 answers
·
asked by
Anonymous
in
Computers & Internet
➔ Programming & Design
EDIT:
Ok getting really close here. Changed the code to this:
array = [12, 16, 31, 53, 70]
new = []
for x in range(0,5):
y = array[x] - array[x-1]
new.append(y)
now i'm getting
[-58, 4, 15, 22, 17]
I'll keep working on it, thanks for your response
2006-07-05
18:11:23 ·
update #1
array = [12, 16, 31, 53, 70]
new = []
new.append(array[0])
for x in range(1,5):
y = array[x] - array[x-1]
new.append(y)
Guess its worked out
2006-07-05
18:22:53 ·
update #2