1 | initial version |

I think I just found the answer: sorry I don't know how to link to it properly!!!

answered Mar 2 '15

calc314 gravatar image

4051 ●19 ●44 ●107

This is really a matter of how Python handles assignments of lists. In both of your append commands, Python is pointing to the same list L. This is why the first list that you append appears to change. The append command here does not actually make a new copy of the list L and then put it in M. Instead, both append commands put references to the original list L in the new list M. To append a new copy of the list L, you could use: M.append(copy(L)) or M.append(L[:]).

link Comments That's very helpful, thanks!

2 | No.2 Revision |

I think I just found the answer: sorry I don't know how to link to it properly!!!

ikol 143 ●2 ●7 ●15 updated Mar 2 '15

calc314 gravatar image calc314 4051 ●19 ●44 ●107

answered Mar 2 '15

calc314 gravatar image

4051 ●19 ●44 ●107

This is really a matter of how Python handles assignments of lists. In both of your append commands, Python is pointing to the same list L. This is why the first list that you append appears to change. The append command here does not actually make a new copy of the list L and then put it in M. Instead, both append commands put references to the original list L in the new list M. To append a new copy of the list L, you could use: M.append(copy(L)) or M.append(L[:]).

link Comments That's very helpful, thanks!

3 | No.3 Revision |

I think I just found ~~the answer: ~~what the problem is: "This is really a matter of how Python handles assignments of lists."
sorry I don't know how to link to it ~~properly!!!~~properly!!!
But I still don't know the solution as my situation is a bit different!!!!!!

ikol 143 ●2 ●7 ●15 updated Mar 2 '15

calc314 gravatar image calc314 4051 ●19 ●44 ●107

answered Mar 2 '15

calc314 gravatar image

4051 ●19 ●44 ●107

This is really a matter of how Python handles assignments of lists. In both of your append commands, Python is pointing to the same list L. This is why the first list that you append appears to change. The append command here does not actually make a new copy of the list L and then put it in M. Instead, both append commands put references to the original list L in the new list M. To append a new copy of the list L, you could use: M.append(copy(L)) or M.append(L[:]).

link Comments That's very helpful, thanks!

4 | No.4 Revision |

I think I just found what the problem is: "This is really a matter of how Python handles assignments of lists."
sorry I don't know how to link to it ~~properly!!!
But I still don't know the solution as my situation is a bit different!!!!!!~~properly!!!

Here is the solution:

L1=[1,2]

L2=[3,4]

L1

L2

L1.extend(L2)

L1

L2

L3=L1[:]

L3

L1.append(8)

L1

L3

output:

[1, 2]

[3, 4]

[1, 2, 3, 4]

[3, 4]

[1, 2, 3, 4]

[1, 2, 3, 4, 8]

[1, 2, 3, 4]

Here is the explanation:

ikol 143 ●2 ●7 ●15 updated Mar 2 '15

calc314 gravatar image calc314 4051 ●19 ●44 ●107

answered Mar 2 '15

calc314 gravatar image

4051 ●19 ●44 ●107

link Comments That's very helpful, thanks!

5 | No.5 Revision |

I think I just found what the problem is: "This is really a matter of how Python handles assignments of ~~lists."
sorry ~~lists."

Here is the solution:

L1=[1,2]

L2=[3,4]

L1

L2

L1.extend(L2)

L1

L2

L3=L1[:]

L3

L1.append(8)

L1

L3

output:

[1, 2]

[3, 4]

[1, 2, 3, 4]

[3, 4]

[1, 2, 3, 4]

[1, 2, 3, 4, 8]

[1, 2, 3, 4]

Here is the explanation:
(sorry I don't know how to link to it ~~properly!!!~~

Here is the solution:

L1=[1,2]

L2=[3,4]

L1

L2

L1.extend(L2)

L1

L2

L3=L1[:]

L3

L1.append(8)

L1

L3

output:

[1, 2]

[3, 4]

[1, 2, 3, 4]

[3, 4]

[1, 2, 3, 4]

[1, 2, 3, 4, 8]

[1, 2, 3, 4]

Here is the explanation:properly!!!)

ikol 143 ●2 ●7 ●15 updated Mar 2 '15

calc314 gravatar image calc314 4051 ●19 ●44 ●107

answered Mar 2 '15

calc314 gravatar image

4051 ●19 ●44 ●107

link Comments That's very helpful, thanks!

6 | No.6 Revision |

I think I just found what the problem is: "This is really a matter of how Python handles assignments of lists."

Here is the solution:

L1=[1,2]

L2=[3,4]

L1

L2

L1.extend(L2)

L1

L2

L3=L1[:]

L3

L1.append(8)

L1

L3

output:

`sage: L1 = [1, `~~2] ~~2]
sage: L2 = [3, ~~4]~~

4]
sage: L1
[1, 2]
sage: L2
[3, 4]
sage: L1.extend(L2)
sage: L1
[1, 2, 3, ~~4]~~

4]
sage: L2
[3, ~~4]~~

4]
sage: L3 = L1[:]
sage: L3
[1, 2, 3, ~~4]~~

4]
sage: L1.append(8)
sage: L1
[1, 2, 3, 4, ~~8]~~

8]
sage: L3
[1, 2, 3, 4]

~~[1, 2, 3, 4]~~I found the explanation in @calc314's answer to Ask Sage question 25998 by @ikol.

Here is the explanation: (sorry I don't know how to link to it properly!!!)

ikol 143 ●2 ●7 ●15 updated Mar 2 '15

calc314 gravatar image calc314 4051 ●19 ●44 ●107

answered Mar 2 '15

calc314 gravatar image

4051 ●19 ●44 ●107

link Comments That's very helpful, thanks!