ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 06 Mar 2017 22:30:58 -0600Overwriting...?http://ask.sagemath.org/question/36845/overwriting/Hello, guys! ... First of all, I'm very new to sage and this forum, so please be generous for my question, please...
So, I've wrote in sage (between @@@@@'s):
@@@@@
p=[3,2,1];
q=p;
temporary=q[0];
q[0]=q[1];
q[1]=temporary;
@@@@@
Then, the result for p and q was
p=[2,3,1] and q=[2,3,1].
But, what I actually want is that p doesn't change; so I want to have
p=[3,2,1] and q=[2,3,1]
so that p doesn't get changed when we set 'q=p', while q is defined to be p when we set 'q=p' and then q get changed via the codes followed by 'q=p' above... In other words, is there a way to 'overwrite' q to be p, and not changing p? Thank you!!!Mon, 06 Mar 2017 19:39:41 -0600http://ask.sagemath.org/question/36845/overwriting/Comment by kcrisman for <p>Hello, guys! ... First of all, I'm very new to sage and this forum, so please be generous for my question, please...</p>
<p>So, I've wrote in sage (between @@@@@'s):</p>
<p>@@@@@</p>
<p>p=[3,2,1];</p>
<p>q=p;</p>
<p>temporary=q[0];</p>
<p>q[0]=q[1];</p>
<p>q[1]=temporary;</p>
<p>@@@@@</p>
<p>Then, the result for p and q was </p>
<p>p=[2,3,1] and q=[2,3,1].</p>
<p>But, what I actually want is that p doesn't change; so I want to have </p>
<p>p=[3,2,1] and q=[2,3,1] </p>
<p>so that p doesn't get changed when we set 'q=p', while q is defined to be p when we set 'q=p' and then q get changed via the codes followed by 'q=p' above... In other words, is there a way to 'overwrite' q to be p, and not changing p? Thank you!!!</p>
http://ask.sagemath.org/question/36845/overwriting/?comment=36846#post-id-36846Just as a gentle FYI, if you highlight things and then click on the "010101" button, it will highlight code, rather than your @ solution.Mon, 06 Mar 2017 19:45:08 -0600http://ask.sagemath.org/question/36845/overwriting/?comment=36846#post-id-36846Answer by nbruin for <p>Hello, guys! ... First of all, I'm very new to sage and this forum, so please be generous for my question, please...</p>
<p>So, I've wrote in sage (between @@@@@'s):</p>
<p>@@@@@</p>
<p>p=[3,2,1];</p>
<p>q=p;</p>
<p>temporary=q[0];</p>
<p>q[0]=q[1];</p>
<p>q[1]=temporary;</p>
<p>@@@@@</p>
<p>Then, the result for p and q was </p>
<p>p=[2,3,1] and q=[2,3,1].</p>
<p>But, what I actually want is that p doesn't change; so I want to have </p>
<p>p=[3,2,1] and q=[2,3,1] </p>
<p>so that p doesn't get changed when we set 'q=p', while q is defined to be p when we set 'q=p' and then q get changed via the codes followed by 'q=p' above... In other words, is there a way to 'overwrite' q to be p, and not changing p? Thank you!!!</p>
http://ask.sagemath.org/question/36845/overwriting/?answer=36847#post-id-36847That's how python works. You can use
q=copy(p)
or another popular idiom in python is to take a "slice" of a list, which is a copy, that happens to cover the whole list:
q=p[:]Mon, 06 Mar 2017 22:30:58 -0600http://ask.sagemath.org/question/36845/overwriting/?answer=36847#post-id-36847