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

Inconsistent list/pointer problem

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
7 1426
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

"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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Jerzy Karczmarczuk | last post by:
Gurus, before I am tempted to signal this as a bug, perhaps you might convince me that it should be so. If I type l=range(4) l.extend() l gives , what else... On the other hand, try
7
by: Evangelista Sami | last post by:
Hi all i have implemented a list type as an array of pointer like this typedef struct { int nb_elements; void **elements; } list; to avoid having a pointer for each element as it is done...
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
1
by: Tim | last post by:
I can't seem to figure out why this very simple linked list wont build.. I mean, there is no intelligence, just add to end. Anyway, please let me know if something i can do will make head (the...
57
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
27
by: jimbo | last post by:
Could someone comment on this code please. 1. Are the comments in it correct? 2. Why does sizeof(arr) not work consistently in C? In someFunction() sizeof(arr) means sizeof(&arr) in main. ...
20
by: Francine.Neary | last post by:
I am learning C, having fun with strings & pointers at the moment! The following program is my solution to an exercise to take an input, strip the first word, and output the rest. It works fine...
17
by: Dudely | last post by:
My web page displays just fine under IE7, but under IE6 about 90% of it is just plain missing. I get the top of the page, and the bottom of the page... but not the middle. I have not tested with...
5
by: Abhishek Bhatt | last post by:
I need to import data programatically from spreadsheet file into database table. There are 30 such tables. User can upload multiple files at a time. For a table(which does not have any referential...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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?

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.