I’m stunned with this function I’ve written for python. I’ve observed that in the lower while it changes the values in the list vctor even though that list is not touched along the function. I’ve passed the pair (10, [1,3,5,7,10]) and it has changed the list vctor to [1,3,5,8,10]. Is there an explanation for this?
def siguiente(k,vctor):
l = len(vctor)
vctorsig = vctor
i = l-1
while i>= 0:
if vctorsig[i] <= k - l + i:
j=i
while j<=l-1:
print vctor
vctorsig[j] = vctor[i]+j-i+1
j=j+1
i = -1
else:
i = i-1
return vctorsig
When you do
vctorsig = vctor, you are makingvctorsiga reference to the list referenced byvctor, so when you modify it, you modify the original list.If you wish to copy the list there, you can simply do
vctorsig = list(vctor).