Ask Your Question
0

Overwriting...?

asked 2017-03-07 02:39:41 +0100

sssageeee gravatar image

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!!!

edit retag flag offensive close merge delete

Comments

Just as a gentle FYI, if you highlight things and then click on the "010101" button, it will highlight code, rather than your @ solution.

kcrisman gravatar imagekcrisman ( 2017-03-07 02:45:08 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2017-03-07 05:30:58 +0100

nbruin gravatar image

That'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[:]
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2017-03-07 02:39:41 +0100

Seen: 320 times

Last updated: Mar 07 '17