ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 21 Nov 2018 01:21:05 -0600Question (and answer) about deepcopy in Sagehttps://ask.sagemath.org/question/44342/question-and-answer-about-deepcopy-in-sage/This was driving me crazy but I just found the answer, slightly different from what I found in a link below. I will explain.
I hope it may be useful to other beginners!
If you have a list of numbers and want to change one, but save the old list, no problem;
you can use = as follows:
T=[1,2,3]
S=T
S[0]=5
now you find that S= [5,2,3] while T= [1,2,3]
If you do this with a list of lists, changing T changes S also, as they are identified. The way to solve this is with the
copy command, either
S=T[:]
or
S=copy(T).
However, if T is a list of lists of lists, which is very easy to come across, you are in trouble.
There is a Python command called "deepcopy" which deals with this.
See
https://stackoverflow.com/questions/28684154/python-copy-a-list-of-lists
The point I just discovered is that to use this instead of e.g. S=copy.deepcopy(T) as suggested in the link --
this gave me an error message!!! --maybe it's ok in Pythin?? what works in SAGEMATH is simply S=deepcopy(T).
example:
T=[]
S=[]
T=[[1],[2],[3]]
S=T[:]
(or equivalently S=copy(T) )
then if you do
S[0][0]=5
now both S and T are [[5],[2],[3]].
if instead you do
S= deepcopy(T)
as suggested by the links (slight modification)
then this works!!! The explanation for why is in the links.Mon, 19 Nov 2018 18:55:41 -0600https://ask.sagemath.org/question/44342/question-and-answer-about-deepcopy-in-sage/Comment by slelievre for <p>This was driving me crazy but I just found the answer, slightly different from what I found in a link below. I will explain.
I hope it may be useful to other beginners!</p>
<p>If you have a list of numbers and want to change one, but save the old list, no problem;
you can use = as follows:</p>
<p>T=[1,2,3]</p>
<p>S=T</p>
<p>S[0]=5</p>
<p>now you find that S= [5,2,3] while T= [1,2,3]</p>
<p>If you do this with a list of lists, changing T changes S also, as they are identified. The way to solve this is with the
copy command, either</p>
<p>S=T[:]</p>
<p>or </p>
<p>S=copy(T).</p>
<p>However, if T is a list of lists of lists, which is very easy to come across, you are in trouble.
There is a Python command called "deepcopy" which deals with this. </p>
<p>See
<a href="https://stackoverflow.com/questions/28684154/python-copy-a-list-of-lists">https://stackoverflow.com/questions/2...</a></p>
<p>The point I just discovered is that to use this instead of e.g. S=copy.deepcopy(T) as suggested in the link --
this gave me an error message!!! --maybe it's ok in Pythin?? what works in SAGEMATH is simply S=deepcopy(T).</p>
<p>example:</p>
<p>T=[]
S=[]</p>
<p>T=[[1],[2],[3]]</p>
<p>S=T[:]</p>
<p>(or equivalently S=copy(T) )</p>
<p>then if you do</p>
<p>S[0][0]=5</p>
<p>now both S and T are [[5],[2],[3]].</p>
<p>if instead you do </p>
<p>S= deepcopy(T)</p>
<p>as suggested by the links (slight modification)
then this works!!! The explanation for why is in the links.</p>
https://ask.sagemath.org/question/44342/question-and-answer-about-deepcopy-in-sage/?comment=44362#post-id-44362Also, please separate the answer from the question. The question should be a question, and the answer should be posted as an answer, for clarity.Wed, 21 Nov 2018 01:21:05 -0600https://ask.sagemath.org/question/44342/question-and-answer-about-deepcopy-in-sage/?comment=44362#post-id-44362Comment by slelievre for <p>This was driving me crazy but I just found the answer, slightly different from what I found in a link below. I will explain.
I hope it may be useful to other beginners!</p>
<p>If you have a list of numbers and want to change one, but save the old list, no problem;
you can use = as follows:</p>
<p>T=[1,2,3]</p>
<p>S=T</p>
<p>S[0]=5</p>
<p>now you find that S= [5,2,3] while T= [1,2,3]</p>
<p>If you do this with a list of lists, changing T changes S also, as they are identified. The way to solve this is with the
copy command, either</p>
<p>S=T[:]</p>
<p>or </p>
<p>S=copy(T).</p>
<p>However, if T is a list of lists of lists, which is very easy to come across, you are in trouble.
There is a Python command called "deepcopy" which deals with this. </p>
<p>See
<a href="https://stackoverflow.com/questions/28684154/python-copy-a-list-of-lists">https://stackoverflow.com/questions/2...</a></p>
<p>The point I just discovered is that to use this instead of e.g. S=copy.deepcopy(T) as suggested in the link --
this gave me an error message!!! --maybe it's ok in Pythin?? what works in SAGEMATH is simply S=deepcopy(T).</p>
<p>example:</p>
<p>T=[]
S=[]</p>
<p>T=[[1],[2],[3]]</p>
<p>S=T[:]</p>
<p>(or equivalently S=copy(T) )</p>
<p>then if you do</p>
<p>S[0][0]=5</p>
<p>now both S and T are [[5],[2],[3]].</p>
<p>if instead you do </p>
<p>S= deepcopy(T)</p>
<p>as suggested by the links (slight modification)
then this works!!! The explanation for why is in the links.</p>
https://ask.sagemath.org/question/44342/question-and-answer-about-deepcopy-in-sage/?comment=44361#post-id-44361To display blocks of code or error messages, skip a line above and below,
and do one of the following (all give the same result):
- indent all code lines with 4 spaces
- select all code lines and click the "code" button (the icon with '101 010')
- select all code lines and hit ctrl-K
For instance, typing
If we define `f` by
def f(x, y, z):
return x * y * z
then `f(2, 3, 5)` returns `30` but `f(2*3*5)` gives:
TypeError: f() takes exactly 3 arguments (1 given)
produces:
> If we define `f` by
>
> def f(x, y, z):
> return x * y * z
>
> then `f(2, 3, 5)` returns `30` but `f(2*3*5)` gives:
>
> TypeError: f() takes exactly 3 arguments (1 given)
Please edit your question to do that.Wed, 21 Nov 2018 01:20:30 -0600https://ask.sagemath.org/question/44342/question-and-answer-about-deepcopy-in-sage/?comment=44361#post-id-44361