From: Francesc A. <fa...@ca...> - 2006-10-20 10:56:09
|
A Divendres 20 Octubre 2006 11:42, Sebastien Bardeau va escriure: [snip] > I can understand that numpy.scalars do not provide inplace operations > (like Python standard scalars, they are immutable), so I'd like to use > > 0-d Numpy.ndarrays. But: > >>> d =3D numpy.array(a[2],copy=3DFalse) > >>> d +=3D 1 > >>> d > > array(4) > > >>> a > > array([2, 3, 3]) > > >>> type(d) > > <type 'numpy.ndarray'> > > >>> d.shape > > () > > >>> id(d) > > 169621280 > > >>> d +=3D 1 > >>> id(d) > > 169621280 > > This is not a solution because d is a copy since construction time... > My question is: is there a way to get a single element of an array into > a 0-d array which shares memory with its parent array? One possible solution (there can be more) is using ndarray: In [47]: a=3Dnumpy.array([1,2,3], dtype=3D"i4") In [48]: n=3D1 # the position that you want to share In [49]: b=3Dnumpy.ndarray(buffer=3Da[n:n+1], shape=3D(), dtype=3D"i4") In [50]: a Out[50]: array([1, 2, 3]) In [51]: b Out[51]: array(2) In [52]: b +=3D 1 In [53]: b Out[53]: array(3) In [54]: a Out[54]: array([1, 3, 3]) Cheers, =2D-=20 >0,0< Francesc Altet =A0 =A0 http://www.carabos.com/ V V C=E1rabos Coop. V. =A0=A0Enjoy Data "-" |