473,387 Members | 1,420 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,387 software developers and data experts.

List behaviour

Maybe I'm missing something but the latter is not the behaviour I'm
expecting:
a = [[1,2,3,4], [5,6,7,8]]
b = a[:]
b [[1, 2, 3, 4], [5, 6, 7, 8]] a == b True a is b False for i in range(len(b)): .... for x in range(4):
.... b[i][x] = b[i][x] + 10
.... b [[11, 12, 13, 14], [15, 16, 17, 18]] a [[11, 12, 13, 14], [15, 16, 17, 18]]


ActivePython 2.4.2 Build 248 (ActiveState Corp.) based on
Python 2.4.2 (#67, Oct 30 2005, 16:11:18) [MSC v.1310 32 bit (Intel)]
on win32
Windows XP

Marcelo

May 17 '06 #1
4 1422
ba************@gmail.com wrote:
Maybe I'm missing something but the latter is not the behaviour I'm
expecting:
a = [[1,2,3,4], [5,6,7,8]]
b = a[:]
b [[1, 2, 3, 4], [5, 6, 7, 8]] a == b True a is b False for i in range(len(b)): ... for x in range(4):
... b[i][x] = b[i][x] + 10
... b [[11, 12, 13, 14], [15, 16, 17, 18]] a [[11, 12, 13, 14], [15, 16, 17, 18]]
b = a[:]

does clone a, but doesn't make a deepcopy of its contents, which you are
manipulating!

So do
[GCC 4.0.2 20050808 (prerelease) (Ubuntu 4.0.1-4ubuntu8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
poWelcome to rlcompleter2 0.96
for nice experiences hit <tab> multiple times import copy
a = [[10]]
b = copy.deepcopy(a)
b[0][0] = 20
a [[10]] b [[20]]

HTH,

Diez
May 17 '06 #2
Am Mittwoch 17 Mai 2006 17:06 schrieb ba************@gmail.com:
Maybe I'm missing something but the latter is not the behaviour I'm

expecting:
a = [[1,2,3,4], [5,6,7,8]]
b = a[:]
b
[[1, 2, 3, 4], [5, 6, 7, 8]]
a == b
True
a is b
False


Try an:
a[0] is b[0]
and
a[1] is b[1]


here, and you'll see, that [:] only creates a shallow copy. Thus, the lists in
the lists aren't copied, they are shared by the distinct lists a and b.

Hope this clears it up.

--- Heiko.
May 17 '06 #3
When you did:
b = a[:]

b was then a copy of a, rather than just a reference to the same a.
But what does a contain? It contains two sublists -- that is, it
contains references to two sublists. So b, which is now a copy of a,
contains copies of the two references to the same two sublists.

What you need to do instead is:
b = copy.deepcopy(a)

to get you what you actually want.

May 17 '06 #4
Thank you very much. It was clear.

May 17 '06 #5

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

Similar topics

2
by: MTT | last post by:
Hi there I like to know if any class avilable before proceeding on my work. I need a inverted list behaviour for sequential access of elements for both direction (previous(),next()) also...
12
by: Tescobar | last post by:
Over one year ago somebody asked here: how to remove selected elements from list in a loop?. The answer was as follows: for( it = l.begin(); it != l.end; ) { if(...) it = l.erase(it); else...
13
by: na1paj | last post by:
here's a simple linked list program. the DeleteNode function is producing an infinit loop i think, but i can't figure out where.. #include <stdio.h> typedef struct { char *str; //str is a...
31
by: metiu uitem | last post by:
Say you have a flat list: How do you efficiently get , , ] I was thinking of something along the lines of: for (key,number) in list: print key, number
35
by: Thierry Loiseau | last post by:
Hello all, and Happy end year 2005 ! Well, I would like to obtain a list of all JavaScript var statement, With "for...in" perharps ? That is bellow my recent test here, but the problem is...
6
by: Heiko Wundram | last post by:
Hi all! The following PEP tries to make the case for a slight unification of for statement and list comprehension syntax. Comments appreciated, including on the sample implementation. ===...
4
by: Army1987 | last post by:
Given: #include <stdlib.h> typedef struct Node { unsigned char Data; struct Node *Next } node_t, *list_t; list_t list; Does list = list->Next = malloc(sizeof(node));
1
by: Ken Pu | last post by:
Hi all, I observed an interesting yet unpleasant variable scope behaviour with list comprehension in the following code: print print x It outputs:
0
by: Weeble | last post by:
On Jun 20, 11:10 pm, Göran Andersson <gu...@guffa.comwrote: I did it that way because CopyTo has a whole bunch of failure cases to take care of (multi-dimensional array, not enough space,...
18
by: Grant Edwards | last post by:
Could whoever is responsible for the gateway that is grabbing my postings off of Usenet and e-mailing them out please fix the headers in the mail messages so that I don't get the bounce messages?...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.