473,770 Members | 2,630 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1442
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*******@mchs i.comwrote in message
news:tc******** *************** *********@4ax.c om...
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*******@mchs i.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
2036
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
2291
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 with a recursive
5
6062
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 hits the right and left keys to move this insertion point (cursor) Here is the problem:
1
2048
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 root pointer) not be null. /* LINKED LIST DEFINITIONS */ typedef struct a_fnode {
57
4303
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
27
1905
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. The compiler knows how big arr is, so why the difference - esp. as it would be v.useful if sizeof(arr) worked in someFunction as it does in main - so you could use sizeof(arr) in the conditional bit of,say, a for loop in someFunction for example?
20
2623
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 when you give it 2 or more words, but when there's only 1 word the results vary depending on whether it's on Windows or Linux: under MSVC it displays no output (as it should); under gcc/Linux it instead gives "Segmentation fault". Any ideas...
17
2312
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 any other browsers. The page is at www.greengoldcapital.com I use considerable amounts of CSS, and a lot of other things including javascript, SSI, PHP, mySQL, and a partridge in a pear tree. I have a third party application integrated in,...
5
1222
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 integrity relation) the alogorithm is as follow: For each file, do the following operations : a) Read the excel file b) Begin Transaction c) Delete all records from the tables d) INSERT all of the record from excel into the table e) If some...
0
9602
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10237
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10071
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10017
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9882
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7431
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.