By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,903 Members | 1,670 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,903 IT Pros & Developers. It's quick & easy.

Inconsistent list/pointer problem

P: n/a
I am having a problem with the corruption of a list. It occurs only
the first time that I call a function and never happens on subsequent
calls. Any suggestions would be appreciated.

I call the function, passing in a list as the input data. The function
must manipulate and operate on a copy of that list's data, without
altering the list in the calling routine.

def myFunc(listA):
listB = listA
work on & modify listB
return(listB)

The first time this function touches listB, listA is corrupted.
However, I get the right results from the function. On subsequent
calls to the function, listA is not corrupted further and the function
will always return the same wrong results, which would be correct
given the corruption of listA created in the first call.

I concluded that it appears that listB is still pointing at elements
of listA and I need to force Python to reassign those pointers to
point to copies of listA's elements.

I've tried copying listA to a tuple and then change the copy back to a
list. That sometimes works if the input data is a simple list. It does
not work if the input data is a list extracted from a list of lists.

listB = tuple(listA)
listB = list(listB)

I've tried building the copy of listA, element by element, but that
doesn't work.

listB = []
for x in listA:
listB.append(x)

I finally had to do some type changing during the element by element
copy and that does seem to work.

Thanks in advance for any suggestions. Doug
Feb 1 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Doug Stell:
The standard module copy has deepcopy, it's slow but it may be a
simple solution to your problem. A better solution is to look where
data is changed and fix that.

Bye,
bearophile

Feb 1 '07 #2

P: n/a

"Doug Stell" <ce*******@mchsi.comwrote in message
news:tc********************************@4ax.com...
I call the function, passing in a list as the input data. The function
must manipulate and operate on a copy of that list's data, without
altering the list in the calling routine.
Then you will want to make a copy:
listB = copy.deepcopy( listA)
Feb 1 '07 #3

P: n/a
Doug Stell a écrit :
I am having a problem with the corruption of a list. It occurs only
the first time that I call a function and never happens on subsequent
calls. Any suggestions would be appreciated.

I call the function, passing in a list as the input data. The function
must manipulate and operate on a copy of that list's data, without
altering the list in the calling routine.

def myFunc(listA):
listB = listA
work on & modify listB
return(listB)
return is a statement, not a function. Please remove these useless (and
possibly harmful) parens.
The first time this function touches listB, listA is corrupted.
It's not. It's just that you did *not* copy listA - you just made listB
reference the same object.
>
I concluded that it appears that listB is still pointing at elements
of listA
It's even worse : both names listA and listB are pointing to the exact
same object.

listA = ['A', 'B', 'C']
listB = listA
assert listA is listB
and I need to force Python to reassign those pointers
s/pointers/references/
to
point to copies of listA's elements.
copy.deepcopy() is your friend then.

Feb 1 '07 #4

P: n/a
En Thu, 01 Feb 2007 10:23:16 -0300, Doug Stell <ce*******@mchsi.com>
escribió:
I am having a problem with the corruption of a list. It occurs only
the first time that I call a function and never happens on subsequent
calls. Any suggestions would be appreciated.

I call the function, passing in a list as the input data. The function
must manipulate and operate on a copy of that list's data, without
altering the list in the calling routine.

def myFunc(listA):
listB = listA
work on & modify listB
return(listB)
This article will help you understanding this behavior:
http://effbot.org/zone/python-objects.htm

--
Gabriel Genellina

Feb 1 '07 #5

P: n/a
def myFunc(listA):
listB = listA
work on & modify listB
return(listB)
def my_func(listA):
listB = listA[:]
#work on & modify listB
return listB

Attribution makes the name t the left 'point' to the result of the
expression at the right.
In your myFunc the expersion at the right is the name listA, at it
willl eval to the list that this references. So you will end with to
references.
In my my_func, the expression at the right is a slicing of the list,
from begin to end (listA[:]). Slicing returns a new list, in this
example, with the same items of the original list. So you end with two
lists, as you wanted.
--
EduardoOPadoan (eopadoan->altavix::com)
Bookmarks: http://del.icio.us/edcrypt
Blog: http://edcrypt.blogspot.com
Jabber: edcrypt at jabber dot org
ICQ: 161480283
GTalk: eduardo dot padoan at gmail dot com
MSN: eopadoan at altavix dot com
--
EduardoOPadoan (eopadoan->altavix::com)
Bookmarks: http://del.icio.us/edcrypt
Blog: http://edcrypt.blogspot.com
Jabber: edcrypt at jabber dot org
ICQ: 161480283
GTalk: eduardo dot padoan at gmail dot com
MSN: eopadoan at altavix dot com
Feb 2 '07 #6

P: n/a
Eduardo "EdCrypt" O. Padoan a écrit :
>def myFunc(listA):
listB = listA
work on & modify listB
return(listB)

def my_func(listA):
listB = listA[:]
#work on & modify listB
return listB

Won't do for the OP's needs - he wants to modify the objects contained
in listB without impacting the ones in listA (or at least that's what I
understand).

Feb 2 '07 #7

P: n/a
Won't do for the OP's needs - he wants to modify the objects contained
in listB without impacting the ones in listA (or at least that's what I
understand).

Sorry. That is true - the items referenced on the [:] copy are the same as
in the original. Rereading what the OP msg, I think we agree.
Feb 2 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.