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

Python List Issue

I've hit a brick wall on something that I'm guessing is pretty simple but
it's driving me nuts. I noticed that with python lists, generally when you
make a copy of a list (ie, List1 = List2) List1 just becomes a reference to
List2 and any modifications done to List1 affects List2. Ok I can live with
this but I want to make a completely seperate copy not attached to the
original in anyway. So then I used this method. List1 = List2[:] . This
seemed to work in most situations, here I can modifiy List1 with out it
affecting List2. But now I've run into an odd problem, and I have no idea
why it's doing what it's doing.
Here's some code

#some globle varibles
bob = [[[0, 0]]]
final = []

########################
def ApplySourceFunc(bob, final):
for I in range(1):

for I in range(len(bob)): #will go through the list and apply
ApplyOperatorsLoop(bob[i][:]) #all the operators to each rule in
the list creating a new list of rules

bob = final
final = []
#end ApplySourceFunc######

########################
def ApplyOperatorsLoop(aList):

iTemp = [] #initial temp, just for validity checking

iTemp = AddGetSpeed(aList[:])
if not iTemp == None: #if the operator returns None, then nothing of
value happend
final.append(iTemp)

#end ApplyOperatorsLoop####

#########################
def AddGetSpeed(tList):
ln = len(tList) #get the length of tList

if ln > 0:
if tList[ln-1][0] == 0:
tList[ln-1][0] = "GetSpeed()"
print "New Rule 'GetSpeed()' Added to the List"
return tList
return None
#end AddGetSpeed#########

So, here's what going on. ApplySourceFunc calls ApplyOperatorsLoop which
calls AddGetSpeed. Each function passes the list bob to the next fuction
using the [:] method. For some reason when I get to the AddGetSpeed method
and I do the assignment " tList[ln-1][0] = "GetSpeed()" " this directly
modifies the list bob. Ok, so i was annoyed by that since I was using the
whole [:] method as I passed and used the lists. Then I tried to insert a
temp list into the ApplyOperatorLoop function ( temp2 = aList[:]) and then
pass that into AddGetSpeed. Even when I did this AddGetSpeed modified the
orginial bob list. so, what I'm getting down to is:
How on earth can I make a complete seperate copy of a list with out it
being a attached to the original in any way shape or form so that I can
modifiy if at will and not worry about the original? I've tried every
combonation that I can think of to get this to work, I even wrote my own
copy function but even then I had the same trouble. Until this point
everything was going good, but this has really bugged me

Any ideas, suggestions, comments are greatly appreciated
thanks

Nick
Jul 18 '05 #1
5 1407
Nick L wrote:
I've hit a brick wall on something that I'm guessing is pretty simple but
it's driving me nuts. I noticed that with python lists, generally when you
make a copy of a list (ie, List1 = List2) List1 just becomes a reference to
List2 and any modifications done to List1 affects List2. Ok I can live with
this but I want to make a completely seperate copy not attached to the
original in anyway. So then I used this method. List1 = List2[:] . This
seemed to work in most situations, here I can modifiy List1 with out it
affecting List2. But now I've run into an odd problem, and I have no idea
why it's doing what it's doing.
Here's some code

#some globle varibles
bob = [[[0, 0]]]


I haven't gone through all of your code, but I suspect that the problem
is that objects in the list (in this case, other lists) don't get copied
only get referenced when you do bob[:]. This is expected.

See copy.deepcopy(). It will make sure that everything gets copied and
nothing just referenced (more or less).

http://docs.python.org/lib/module-copy.html

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
Jul 18 '05 #2
See copy.deepcopy(). It will make sure that everything gets copied and
nothing just referenced (more or less).


So far copy.deepcopy() seems to be working perfectly.
Thanks for the input

Nick
Jul 18 '05 #3
On Sun, 27 Mar 2005 09:01:20 GMT, "Nick L" <Fe********@mchsi.com>
wrote:
I've hit a brick wall on something that I'm guessing is pretty simple but
it's driving me nuts.
Yes, I've ran across that too a few times.
How on earth can I make a complete seperate copy of a list with out it
being a attached to the original in any way shape or form so that I can
modifiy if at will and not worry about the original?
This routine copies a list of lists.
# Makes a copy of a list of lists
# Containing simple data.
def copylistlist(alist):
if type(alist) is list:
copy = []
for i in alist:
if type(i) is list:
i = copylistlist(i)
copy.append(i)
return copy

bob = [[[0, 0]]]
final = copylistlist(bob)

print 'bob:'bob
print 'Final:'final
This still doesn't create new items within the new list. but with
literal data consisting of letters and numbers, it will work.

If you are working with a data tree, you may be able to modify this to
do what you want. Just add a test in the inner loop for the data you
want to modify.

Any ideas, suggestions, comments are greatly appreciated
thanks

Nick


Hope that helps.

Ron_Adam
Jul 18 '05 #4
Thanks, thats a really handy function
"Ron_Adam" <ra****@tampabay.rr.com> wrote in message
news:mt********************************@4ax.com...
On Sun, 27 Mar 2005 09:01:20 GMT, "Nick L" <Fe********@mchsi.com>
wrote:
I've hit a brick wall on something that I'm guessing is pretty simple but
it's driving me nuts.


Yes, I've ran across that too a few times.
How on earth can I make a complete seperate copy of a list with out itbeing a attached to the original in any way shape or form so that I can
modifiy if at will and not worry about the original?


This routine copies a list of lists.
# Makes a copy of a list of lists
# Containing simple data.
def copylistlist(alist):
if type(alist) is list:
copy = []
for i in alist:
if type(i) is list:
i = copylistlist(i)
copy.append(i)
return copy

bob = [[[0, 0]]]
final = copylistlist(bob)

print 'bob:'bob
print 'Final:'final
This still doesn't create new items within the new list. but with
literal data consisting of letters and numbers, it will work.

If you are working with a data tree, you may be able to modify this to
do what you want. Just add a test in the inner loop for the data you
want to modify.

Any ideas, suggestions, comments are greatly appreciated
thanks

Nick


Hope that helps.

Ron_Adam

Jul 18 '05 #5
Nick L wrote:
I noticed that with python lists, generally when you
make a copy of a list (ie, List1 = List2) List1 just becomes a reference to
List2 and any modifications done to List1 affects List2. Ok I can live with
this but I want to make a completely seperate copy not attached to the
original in anyway.
Although your immediate question has been answered (you wanted
a deep copy, not a shallow one), I think you should take a
step back and consider whether you really need to do that much
copying in the first place.

With the proper approach, I find it's extremely rare to need to
copy anything in a Python program, and rarer still to need
deep copying. Generally, it's better for a function not to
modify objects passed to it, unless that's the purpose of
the function. If a function needs a local copy of an argument,
it should create one itself, and not put the burden on the
caller.

Also, there are some things about your code that suggest you
still do not fully understand how scoping and assignment work
in Python:
def ApplySourceFunc(bob, final):
...
bob = final
final = []
These last two statements do not accomplish anything useful.
All they do is change which objects are referred to by the
parameter names 'bob' and 'final', which are local to the
function, and have no effect on anything outside it.
def ApplyOperatorsLoop(aList):
...
final.append(iTemp)
...


Here you are referring to the global variable 'final', not
the one passed as a parameter to ApplySourceFunc(). To get
the effect you seem to be after, you would need to pass
'final' on as a parameter to ApplyOperatorsLoop().

Here is how I would write this:

def ApplySourceFunc(bob, final):
for item in bob:
ApplyOperatorsLoop(item, final)

def ApplyOperatorsLoop(item, final):
new_item = AddGetSpeed(item)
if new_item:
final.append(temp)

def AddGetSpeed(tList):
if tList and tList[-1][0] == 0:
new_tList = tList[:]
new_tList[-1] = new_tList[-1][:]
new_tList[-1][0] = "GetSpeed()"
print "New Rule 'GetSpeed()' Added to the List"
return new_tList
else:
return None

Notice how all the copying is done inside AddGetSpeed(),
it only copies what is needed, and the fact that the copying
is needed is encapsulated within the function; its callers
don't need to know.

I have also made use of some other common Python idioms:

* Whenever possible, it is simpler and clearer to iterate
directly over the items of a list, rather than iterating
over its indices and then indexing it.

* Almost any object can be used as a truth value. Non-empty
lists count as true; empty lists and None count as false.

* The 'and' and 'or' operators short-circuit: if the first
operand determines the result, the second operand is not
evaluated.

* Negative list indices are counted from the end of the
list; e.g. aList[-1] means the last item of aList.

Hope that helps,

--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg
Jul 18 '05 #6

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

Similar topics

68
by: Lad | last post by:
Is anyone capable of providing Python advantages over PHP if there are any? Cheers, L.
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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.