-1

Here is my code:

x = [[1],[1],[1],[-1]]
a = [[-2,-1,-1,-2],[1,-2,2,-1],[-1,-2,-2,-1],[2,-1,1,-2]]
h = 0.1
def K(h,a,x):
    cont = [1,2,3,4]
    k = []
    for i in range(len(cont)):
        k.append(h*cont[i])
    y = x
    print('original value',x)
    for j in range(len(y)):
        y[j][0] += k[j]/4
        print('modified value',x)
K(h,a,x)

So the question is why did the value of x change if it has not received anything?

viraptor
  • 33,322
  • 10
  • 107
  • 191
  • 1
    Because you changed `y` and `y` is just `x`. To copy a value you need to actually copy it `y = copy.deepcopy(x)`. – MSeifert Jun 08 '17 at 01:17
  • but it does not make sense to me,for exemple, if i put x = 3, y = x and y+= 5, the valeu of x will not change – Daniel Fang Jun 08 '17 at 01:19
  • this strange behavior applies to lists, not to integers. That's why x = 3... behaves as you expect. –  Jun 08 '17 at 01:21
  • `y += 5` is like `y = y + 5`. `y + 5` creates a new value, which you then assign to `y`. In this case, no new value is being created; you’re just modifying the same list. – Ry- Jun 08 '17 at 01:21
  • @DanielFang I answered a similar question yesterday: https://stackoverflow.com/a/44401491/5393381 You might want to take a look. Also `y+=5` doesn't work in-place for integers. For integers it's just a short form of `y = y + 5`. – MSeifert Jun 08 '17 at 01:21
  • @JulianCienfuegos: That’s extremely misleading. Integers are immutable, and `y = y + [5]` wouldn’t change a list `y`. – Ry- Jun 08 '17 at 01:21
  • @DanielFang In general you could avoid doing all the in-place changes and just use comprehensions to create the new values and return them: https://gist.github.com/MSeifert04/cf526882ddb94bf0cb9f428a9a738814 – MSeifert Jun 08 '17 at 01:29
  • so, it occurs just with list, and there is no problem if i do the same with integers,right? – Daniel Fang Jun 08 '17 at 01:30
  • No, it will happen with almost everything that is not explicitly immutable. It's more that numbers (but also strings) are the exception. – MSeifert Jun 08 '17 at 01:34
  • @MSeifert i changed y = x to y = x[:] but it does not work – Daniel Fang Jun 08 '17 at 01:42
  • I Know that it doesn't work. I already gave you two working solutions. Just use one of These. – MSeifert Jun 08 '17 at 01:52
  • check my edit, I found the problem – syntaxError Jun 08 '17 at 01:55
  • @MSeifert i also tried y = copy.deepcopy(x), but it says 'NameError: name 'copy' is not defined' – Daniel Fang Jun 08 '17 at 01:55
  • @rosh i put y = deepcopy(x) but it results in NameError: name 'deepcopy' is not defined – Daniel Fang Jun 08 '17 at 01:59
  • you need to import it, 'from copy import deepcopy' - put that at the top and it will work. – syntaxError Jun 08 '17 at 02:00
  • 1
    @rosh thanks! i got it – Daniel Fang Jun 08 '17 at 02:01
  • No problem, super easy to miss. Would appreciate it if you could accept my answer. – syntaxError Jun 08 '17 at 02:02

2 Answers2

1

When you put:

y = x

All x is doing is acting as a pointer to the list object in memory. When you do the above, you are saying I want y to point to the same list that x references in the memory. Any changes in place changes to y will also affect x.

If you wish to have the reference y point to a different object in memory (with the same values as x,) you need to make a copy of x.

y = x[:] #make a copy.  SEE EDIT

Now y and x will be pointing to different list objects in memory, but both objects will have the same values.

Note that any mutable datatype in python shares this reference property, such as: sets, dictionaries, lists, byte arrays and also some classes.

Look into the differences between mutable and immutable datatypes in python as this distinction is critical and will lead to undiagnosible bugs without knowledge on how python accesses different data types.

EDIT!!

Sorry, I did not notice x is made of a series of lists. You need to use deepcopy to copy the nested lists!

from copy import deepcopy
y = deepcopy(x)
syntaxError
  • 896
  • 8
  • 15
0

When you write:

y = x

You're literally saying that x equals y. This doesn't make a copy of x. Whatever you do to x will happen to y and vice versa. You need to make a copy of x if you want them to be independent.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117