[Tutor] reference instead of copy
Tim Johnson
tim at johnsons-web.com
Fri Oct 17 23:25:51 EDT 2003
Hello All:
I have been using the following function
(produced by help from this list (thanks!))
which returns a subset of a list.:
def extract (oldlist, spacing,ndx=-1):
if ndx > -1:
return [oldlist[i][ndx] for i in range (len(oldlist)) if not i%spacing]
else:
return [oldlist[i] for i in range (len(oldlist)) if not i%spacing]
# But let's suppose I wish to operate on this subset and have it
# reflected in the original list. I can't do this with the above
# configuration. Example below:
>>> test = [1,2,3,4,5,6,7,8,9,10]
>>> t = mylib.extract(test,2)
>>> print t
[1, 3, 5, 7, 9]
>>> t[1] = 'three'
>>> print t
[1, 'three', 5, 7, 9]
>>> print test
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# What I need for this is and 'extract' function which retains
# the original reference so that when I code:
t[1] = 'three'
# list 'test looks like this:
[1, 2, 'three', 4, 5, 6, 7, 8, 9, 10]
Any ideas?
TIA
tim
--
Tim Johnson <tim at johnsons-web.com>
http://www.alaska-internet-solutions.com
More information about the Tutor
mailing list