Can get reference of a part of a list?

F. Petitjean littlejohn.75 at news.free.fr
Thu May 12 13:16:17 EDT 2005


Le Fri, 13 May 2005 00:58:49 +0800, flyaflya a écrit :
> I want make a 2-D array from a list,all elements is references of 
> list's,like this:
> a = [1,2,3,4]
> b = [ [1,2], [3,4] ]
> when change any elements of a, the elements of b will change too, so I 
> can  use some function for list to change b.
> c/c++ can work in this way,I think let b[0] = a[0:2], b[1] = a[2:3],but 
>     it's not reference,it's copy.
If the original list contains only numeric values of the same type, i.e
only integers or only floats, the Numeric array reshape() function is
what you need :

import Numeric as N
aa = N.array([1, 2, 3, 4])
bb = N.reshape(aa, (2, 2))
print bb
array([[1, 2],
       [3, 4]])
bb[0][:] = (7, 8)
aa
array([7, 8, 3, 4])
That is there is only one homogeneous list of values, no copy.



More information about the Python-list mailing list