D
Doug Stell
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
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