Slice assignment using Numeric API?

Serge Rey rey at typhoon.sdsu.edu
Wed May 16 23:32:52 EDT 2001


> Suppose "array" is a Numeric array. The statement, in Python, "subarray =
> array[1:4,1:4]" creates a new array without copying the old one. How do I do
> this statement in C using the Numeric API?

a kludge is to use the copy module:

>>> import Numeric
>>> x=Numeric.array([1,2,3])
>>> x
array([1, 2, 3])
>>> c=x
>>> c[2]=4
>>> x
array([1, 2, 4])
>>> import copy
>>> d=copy.copy(x)
>>> d[2]=5
>>> x
array([1, 2, 4])
>>> d
array([1, 2, 5])
>>> c
array([1, 2, 4])
>>> 

i think you could also use the tolist method of numpy arrays: 

>>>f=x.tolist()[:]
>>> f
[1, 2, 4]
>>> f[2]=7
>>> x
array([1, 2, 4])
>>> f
[1, 2, 7]
>>> 

HTH

-- 
Sergio J. Rey	http://typhoon.sdsu.edu/rey.html
"This is not a novel to be tossed aside lightly.  It should be thrown with
great force."                  - Dorothy Parker




More information about the Python-list mailing list