Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > a=b change b a==b true??

Reply
Thread Tools

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

 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      02-26-2007
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
 
Reply With Quote
 
 
 
 
Chris Mellon
Guest
Posts: n/a
 
      02-26-2007
On 26 Feb 2007 05:50:24 -0800,
<> 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.
 
Reply With Quote
 
Philipp Pagel
Guest
Posts: n/a
 
      02-26-2007
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
 
Reply With Quote
 
bayer.justin@googlemail.com
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]]

 
Reply With Quote
 
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

 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      02-27-2007
En Mon, 26 Feb 2007 11:26:43 -0300, <> escribió:

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


Now that you have solved your immediate problem, you could read:
http://effbot.org/zone/python-objects.htm

--
Gabriel Genellina

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Democrat's "change," not pocket change Cyberiade.it Anonymous Remailer Computer Support 0 03-14-2009 07:07 AM
"Change your language and you change your thoughts." Suganya C Programming 0 04-29-2008 01:35 PM
Change the master GridView after detail change? Q. John Chen ASP .Net 0 11-15-2006 05:31 PM
Change the master GridView after detail change? Q. John Chen ASP .Net 0 11-15-2006 05:30 PM
A Paradise DNS address change? What change? There was no change. Tony Neville NZ Computing 7 09-22-2006 01:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57