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

Philipp Pagel pDOTpagel at gsf.de
Mon Feb 26 09:03:14 EST 2007


rstupplebeen at gmail.com 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



More information about the Python-list mailing list