473,395 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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

Feb 26 '07 #1
6 1102
rs**********@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. 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
Feb 26 '07 #2
On 26 Feb 2007 05:50:24 -0800, rs**********@gmail.com
<rs**********@gmail.comwrote:
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.
Feb 26 '07 #3
rs**********@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
Feb 26 '07 #4
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]]

Feb 26 '07 #5
All,
It works great now. Thank you for all of your incredibly quick
replies.
Rob

Feb 26 '07 #6
En Mon, 26 Feb 2007 11:26:43 -0300, <rs**********@gmail.comescribió:
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

Feb 27 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alpha | last post by:
I have 3 radio buttons for include, exclued or 'select all' from the listbox items. If a user selects the 'Select All' button' then all items in listbox is hi-lited as selected. Now, when user...
3
by: dave | last post by:
I have half a dozen web form radio buttons on a web form. Each of them is set to postback=true. However, if for instance radiobutton1 is already selected and the user selects it again, it performs...
3
by: Jim in Arizona | last post by:
I have a gridview that's being populated from an access db query. The problem I'm having is that the date/time fields in access that are populating the gridview are showing both date and time, when...
3
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - I have window.status="Moomin"; why doesn't the statusbar change?...
1
by: Sankalp | last post by:
Hi, I am using VB 2005. My application has many data bound controls. The connection is stored in the app.config file. I want the application to start with a default connection string and while...
4
by: Dan | last post by:
H, i want to use for one specific application the default membership provider as defined by default in machine.config, except one element: i want to use requiresUniqueEmail="false" instead of...
7
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post by:
I want to change the font color and weight at a specific position in the text. I am not sure where or what I need to do. It will be at position 155/156 from the left on each line. Here is the...
2
by: remya1000 | last post by:
i'm using VB.NET. and its a windows application. in my program i need to display a table in datagrid. so i created the table using DataTable and i'm calling that dataTable to dataGrid. and its...
1
by: webandwe | last post by:
Hi, Can somebody please show me how to change the connection so I can make it work with my MYSQL database... I just need this login to work then I'm done wiht my project. I don't know what is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.