On Mon, 2006-06-12 at 09:37 -0400, Kermit Rose wrote: > > From: Python > Date: 06/11/06 22:59:38 > To: Kermit Rose > Cc: Tutor Python > Subject: Re: [Tutor] assignment statements in python > > > The basic python objects: numbers, strings, and tuples are immutable and > can not be changed (mutated). > a = B = 3 > B = 4 # binds B to a different object with value 4 > # the object with value 3 is unchanged > print a > 3 > > ** > If I write > > a = 3 > a = 4 > a = 5 > > Are the objects containing 3 and 4 erased when they no longer have a name?
Yes > > ** > > >>>>> > > Container objects such as lists and dictionaries can be changed in > place. > >>> a = B = [1,2,3] > >>> B.append(4) # changes (mutates) B > >>> print a > [1, 2, 3, 4] > > ****** > > Good. Now I know a more efficient way to extend an array. > > I had been creating an entire new array, and equivalencing the old array to > it. > > ****** > >>>>>> > > >>> B[2] = B[3] # positions 2 and 3 reference the same object > >>> print B > [1, 2, 4, 4] > >>> print a > [1, 2, 4, 4] > > ****** > > I still don't know how to make it so that > > If B = [ 1,2,4,5] > > B.append(value of B[4]) There is no B[4] B[0] is 1 B[1] is 2 B[2] is 4 B[3] is 5 Perhaps you mean to search B looking for the value 4 and then append that value? index_of_4 = B.index(4) # returns index to location of first 4 B.append( B[index_of_4]) # appends the 4 to the end of B > copy the value of B[2] into B[3] >>> import copy >>> B = [ 1,2,4,5] >>> B[3] = copy.copy(B[2]) >>> B [1, 2, 4, 4] Since 4 is immutable, there is no need to use the copy module, but it is there for when you need to make copies of an object. > copy the value 3 into B[2]. B[2] = 3 # no need for a copy since 3 is immutable > > > Or, equivalently, > > If B = [1,2,4,5] > > Insert the value 3 between > B[1] and b[2], > >>> B = [ 1,2,4,5] >>> B.insert(2,3) # inserts 3 before B[2] >>> B [1, 2, 3, 4, 5] >>> help(B.insert) insert(...) L.insert(index, object) -- insert object before index (Use q to leave the help screen) > so that B > becomes > [1,2,3,4,5]. > > Kermit < [EMAIL PROTECTED] > > > > > > > > > _______________________________________________ > > Tutor maillist - Tutor@python.org > > http://mail.python.org/mailman/listinfo/tutor -- Lloyd Kvam Venix Corp _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor