[Tutor] Cloning lists to avoid alteration of original list

Alan Gauld alan.gauld at blueyonder.co.uk
Tue Jan 13 18:02:51 EST 2004


Sorry the formatting on that is so messed up I can't read it.

I do notice a lot of

else: pass

lines though. They are doing nothing but occupying screen space,
you might as well lose them. One advantage of Python's indentation
based syntax is that dangling else problems don't arise. The
disadbvantage is that bad indentation makes the code unreadable!

Alan G.



----- Original Message ----- 
From: "Alistair McGowan" <alistair.mcgowan at ucl.ac.uk>
To: <tutor at python.org>
Sent: Tuesday, January 13, 2004 2:42 PM
Subject: [Tutor] Cloning lists to avoid alteration of original list


> Dear All,
> I am having problems with cloning a list in such a
> way that it alters one list but not the other. The list for the new
> area should have "4"
>
> I have tried the standard procedure of taking a slice of the list,
> renaming it and operatiing on that, but to no avail. One issue that
> may make a difference is that I am taking a slice of a longer list,
> rather than the complete list.
>
> Here is the section of code, which is a class. I have tried cloning
> the two lists outside of the class, as well as within the class, but
> the program always carries out the changes to both lists. I
apologize
> for the length of the extact. If further information is needed,
> please let me know.
> Thanks in advance,
> Al
>
> class mergeTaxa: # geodispersal of taxon lists from colliding areas
> def __init__(self,m,y,clone1,clone2):
> self.mergetaxa=[] #empty list
> self.mergetaxa.append (name)#id of new merged area
> one = clone1
> two = clone2
> print "one", one
> print "two", two
> temp = []# an empty list for listing taxa for comparison
> for e in range (0, len(one)):# starts to loop over
> list 1 and writes to merged list
> x = one[e] #extracts info for each taxon in area
> a = one[e][0]#gets the id #
> if one [e][2]==0: #checks if taxon is alive
> one[e][3]= 4
> print clone1
> self.mergetaxa.append(x)
> temp.append(a)
> else:
> pass
> for j in range (0, len(two)):# starts loop over other list
> p=two[j]
> f = two[j][0]
> if two[j][2]==0:
> z = temp.count(f)# checks if taxon
> occurs in other area list
> if z ==0: #  if not add to new area list
> two[j][3]=4
> self.mergetaxa.append(p)
> else:
> pass
> else:
> pass
> merge = self.mergetaxa
> print "merged", merge
> print clone1
> print clone2
> tbya.append(merge)#writes out to main taxon by area matrix
> -- 
> Alistair J. McGowan
> Department of Earth Sciences,
> University College, London,
> London
>
> WC1E 6BT
>
> Phone: TBA
> Fax: +44 (0)20 7387 1612
>
> "Hope is a duty from which paleontologists are exempt."
> David Quammen
>
> "Empty causes. You've got yours and I've got mine"
> Greg Graffin
>




More information about the Tutor mailing list