# arranges a lists indexes from which ones have the maximum
# value to which ones have the minimum value
def sortIndex(L):
I = []
original = L
new = L
for i in range(len(original)):
I.append(original.index(max(new)))
del new[I[len(I)-1]]
print("I=",I)
print("O=",original)
print("N=",new)
return I
A = [1,6,3,8,4]
print(sortIndex(A))
That's the code. When I run it, I get the output (note I print I, O and N after every iteration):
I= [3]
O= [1, 6, 3, 4]
N= [1, 6, 3, 4]
I= [3, 1]
O= [1, 3, 4]
N= [1, 3, 4]
I= [3, 1, 2]
O= [1, 3]
N= [1, 3]
I= [3, 1, 2, 1]
O= [1]
N= [1]
I= [3, 1, 2, 1, 0]
O= []
N= []
[3, 1, 2, 1, 0]
I don't understand why this is happening. Del seems to be deleting values from both new and original even though they are clearly separate lists. Does anyone know what is going on here?
Because they aren't actually separate lists. Both variables, new
and original
are pointing to the same list in memory. You can create a copy like you want with copy.deepcopy
from copy import deepcopy
original = L
new=deepcopy(original)