a=b change b a==b true??

R

rstupplebeen

I do not have a clue what is happening in the code below.
a=[[2,4],[9,3]]
b=a
[map(list.sort,b)] [[None, None]]
b [[2, 4], [3, 9]]
a
[[2, 4], [3, 9]]

I want to make a copy of matrix a and then make changes to the
matrices separately. I assume that I am missing a fundamental
concept. Any help would be appreciated.

Rob Stupplebeen
 
D

Diez B. Roggisch

I do not have a clue what is happening in the code below.
a=[[2,4],[9,3]]
b=a
[map(list.sort,b)] [[None, None]]
b [[2, 4], [3, 9]]
a
[[2, 4], [3, 9]]

I want to make a copy of matrix a and then make changes to the
matrices separately. I assume that I am missing a fundamental
concept. Any help would be appreciated.


You are missing that the operation

b=a

is not a copying, but a mere name-binding. The object that a points to now
is also pointed at by b.

If you need copies, use the copy-module, with it's method deepcopy.

Diez
 
C

Chris Mellon

I do not have a clue what is happening in the code below.
a=[[2,4],[9,3]]
b=a
[map(list.sort,b)] [[None, None]]
b [[2, 4], [3, 9]]
a
[[2, 4], [3, 9]]

I want to make a copy of matrix a and then make changes to the
matrices separately. I assume that I am missing a fundamental
concept. Any help would be appreciated.

this:

does not make a copy. It binds the name b to the same object that is
bound to the name a. If you want a copy, ask for a copy.
a = [1,2]
import copy
b = copy.copy(a) #shallow copy
a.append(3)
a,b ([1, 2, 3], [1, 2])


You may need to do something else depending on exactly what copy
semantics you want, read the docs on the copy module.
 
P

Philipp Pagel

I do not have a clue what is happening in the code below.
a=[[2,4],[9,3]]
b=a
[map(list.sort,b)] [[None, None]]
b [[2, 4], [3, 9]]
a
[[2, 4], [3, 9]]
I want to make a copy of matrix a and then make changes to the
matrices separately. I assume that I am missing a fundamental
concept.

The concept you are missing is the fact that b = a makes b another name
for your nested list. As you correctly stated you really want a copy.
This will do what you want:
import copy
a=[[2,4],[9,3]]
b = copy.deepcopy(a)
[map(list.sort,b)] [[None, None]]
a [[2, 4], [9, 3]]
b
[[2, 4], [3, 9]]

cu
Philipp
 
B

bayer.justin

If you want to copy lists, you do it by using the [:] operator. e.g.:
a = [1,2]
b = a[:]
a [1, 2]
b [1, 2]
b[0] = 2
a [1, 2]
b
[2, 2]

If you want to copy a list of lists, you can use a list comprehension
if you do not want to use the copy module:
a = [[1,2],[3,4]]
b = [i[:] for i in a]
a [[1, 2], [3, 4]]
b [[1, 2], [3, 4]]
b[0][0] = "foo"
a [[1, 2], [3, 4]]
b
[['foo', 2], [3, 4]]
 
R

rstupplebeen

All,
It works great now. Thank you for all of your incredibly quick
replies.
Rob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top