-2
print(ys)
for i in range(counter):
    if(ys[i]<yzero[i]):
        dupys[i]=yzero[i]
#plt.plot(xs,dupys) #this is used for checking on the levelline graph
print(ys)

I have try many different ways to test it out, but it seems like it changes by itself for no reason, from the code, we can see that the I only change the dupys list.

output:

[-1.401109325007359, -1.4012097591798365, -1.4013107885799283, -1.4013711090958765, -1.4013991265977308,...]

[-1.4, -1.4, -1.4, -1.4, -1.4, -1.4, -1.4, -1.4, -1.4, -1.4, -1.4, -1.4, ...]

1 Answers1

1

Even if a part of the code is missing, I suppose you're facing the following problem:

dear_old_value = ["rusty_value0", "rusty_value1", "rusty_value2", "rusty_value3"]

new_values = dear_old_value

new_values[0] = "shiny_value0"
new_values[2] = "shiny_value2"

print dear_old_value # OUTPUT: ['shiny_value0', 'rusty_value1', 'shiny_value2', 'rusty_value3']

as you see, even if you're modifying values in the new list, also the old one gets modified.
To work around this problem, you should assign to the new list a copy of the old one:

dear_old_value = ["rusty_value0", "rusty_value1", "rusty_value2", "rusty_value3"]

new_values = list(dear_old_value)

new_values[0] = "shiny_value0"
new_values[2] = "shiny_value2"

print dear_old_value # OUTPUT: ['rusty_value0', 'rusty_value1', 'rusty_value2', 'rusty_value3']

In Python, you should think at variable names as tags for objects.
In our case we are not assigning the values contained in dear_old_values to new_values, we are simply saying that the object referenced by dear_old_values will now be also referenced by new_values.
You can check the ID of your object to confirm this:

dear_old_value = ["rusty_value0", "rusty_value1", "rusty_value2", "rusty_value3"]

new_values = dear_old_value

print id(dear_old_value) # 50779920
print id(new_values)     # 50779920

To avoid this, we initialize our variable (new_values) as a new list object (with a different position in memory), then we push the items in dear_old_varables into it.
If you check now the IDs you'll get this result:

dear_old_value = ["rusty_value0", "rusty_value1", "rusty_value2", "rusty_value3"]

new_values = list(dear_old_value)

print id(dear_old_value) # 41342736
print id(new_values)     # 41358240

Keep in mind that all the objects in Python behaves like this.

Gsk
  • 2,929
  • 5
  • 22
  • 29
  • This means that I suppose that somewhere before the code you posted you wrote `dupys = ys` – Gsk Jun 13 '18 at 15:29
  • you are right! I did write 'dupys = ys' before the code. So in python, I should not directly make two lists equal each other? I am kinda confused by how python work and I dont know why this is happening. I mainly used C++ before, this is the first time i program with python. – DefinitelyNotHal Jun 13 '18 at 16:05
  • I've extended the answer, maybe now its not only *working*, but is also clear what is going on! – Gsk Jun 13 '18 at 16:17
  • Thank you so much! – DefinitelyNotHal Jun 13 '18 at 16:35