[Tutor] Copying sequences of arbitrary dimension

Theresa Robinson robinst at MIT.EDU
Wed Jun 30 01:50:24 EDT 2004


I'm trying to pad a sequence of arbitrary dimension with zeros in order to
get a sequence of a given size.  This is what I have so far, as an
intermediate step.  I know exactly why it doesn't work: because things in
python get passed by value, not by reference.  But can anyone suggest how
I might want to tackle this?

>>> def recursecopy(x,y):
...    if type(x) is IntType:
...       y=x
...    else:
...       for i in range(0,len(x)):
...          recursecopy(x[i],y[i])
...
>>> recursecopy(x=A,y=B)
>>> A
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> B
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]




More information about the Tutor mailing list