Velocity Reviews > a=b change b a==b true??

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

rstupplebeen@gmail.com
Guest
Posts: n/a

 02-26-2007
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

Diez B. Roggisch
Guest
Posts: n/a

 02-26-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> 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

Chris Mellon
Guest
Posts: n/a

 02-26-2007
On 26 Feb 2007 05:50:24 -0800, (E-Mail Removed)
<(E-Mail Removed)> wrote:
> 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:
>>> b=a

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.

Philipp Pagel
Guest
Posts: n/a

 02-26-2007
(E-Mail Removed) wrote:
> 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

--
Dr. Philipp Pagel Tel. +49-8161-71 2131
Dept. of Genome Oriented Bioinformatics Fax. +49-8161-71 2186
Technical University of Munich
http://mips.gsf.de/staff/pagel

Guest
Posts: n/a

 02-26-2007
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]]

rstupplebeen@gmail.com
Guest
Posts: n/a

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

Gabriel Genellina
Guest
Posts: n/a

 02-27-2007
En Mon, 26 Feb 2007 11:26:43 -0300, <(E-Mail Removed)> escribió:

> It works great now. Thank you for all of your incredibly quick
> replies.

http://effbot.org/zone/python-objects.htm

--
Gabriel Genellina