Numpy-scalars vs Numpy 0-d arrays: copy or not copy?

Tim Hochberg tim.hochberg at ieee.org
Fri Oct 20 09:44:58 EDT 2006


Francesc Altet wrote:
> 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 = numpy.array(a[2],copy=False)
>>  >>> d += 1
>>  >>> d
>>
>> array(4)
>>
>>  >>> a
>>
>> array([2, 3, 3])
>>
>>  >>> type(d)
>>
>> <type 'numpy.ndarray'>
>>
>>  >>> d.shape
>>
>> ()
>>
>>  >>> id(d)
>>
>> 169621280
>>
>>  >>> d += 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:
[SNIP]

Here's a slightly more concise version of the same idea:

b = a[n:n+1].reshape([])


-tim



-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642




More information about the NumPy-Discussion mailing list