English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

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

2 answers

I'm not a programming expert, but it looks like you've got a double loop. The code:

for x in range(5):
for value in array:

is causing two loops. Thats why you get so many entries in your list. You need two make only one loop. I would use a couple of variables to store the data that will be subtracted.
I would do it like this:

array = [12, 16, 31, 53, 70]
new = []
subtract = 0
for value in array:
{
value = array[x] - subtract
new.append(value)
subtract = subtract + array[x]
}

First we setup a variable to hold what we want to subtract, we start with zero because we don't want to subtract anything. Then we set value to the list item - 'subtract'. we then append the value to the new list and then add the list item to 'subtract' to have it subtract the new value the next round. The 'subtract' variable should keep its value and add the list items like you want.

2006-07-05 16:49:43 · answer #1 · answered by Bryan A 5 · 4 1

the least puzzling "powerful" form for most persons is the insertion form, so that you're able to surely outline a function for it (it really is in 3.a million, so your syntax will be really diverse): def insertion_sort(a): i = 0 at the same time as(i0 and a[j-a million]>temp): a[j] = a[j-a million] j = j - a million a[j]= temp i = i + a million go back a x = [a million,6,3,4,7,2] print("x is initially: ",x) x = insertion_sort(x) print("After sorting, x is: ",x) in case you write down the values of your variables in each and every and each and every step of each and every loop, you could be waiting to be particular really surely how the coolest judgment works. also...yahoo recommendations does not seem to love indentation, so that you'll would favor to operate that lower back in to coach it.

2016-11-05 23:08:59 · answer #2 · answered by Anonymous · 0 0

fedest.com, questions and answers