problems with duplicating and slicing an array

beliavsky at aol.com beliavsky at aol.com
Thu Jan 20 21:21:16 EST 2005


Yun Mao wrote:

>I have some questions when I'm using python numeric:

>1. When I do v = u[:, :], it seems u and v still point to the same
>memory. e.g. When I do v[1,1]=0, u[1,1] will be zero out as well.
>What's the right way to duplicate an array? Now I have to do v =
>dot(u, identity(N)), which is kind of silly.

You can use v = 1*u or v  = u.copy() to get the type of copy you want.

>2. Is there a way to do Matlab style slicing?

Use the take function.

The output of the following code

from Numeric import array,take
a = array([1,2])
b = a
c = 1*a
d = a.copy()
a[0] = 3
print b
print c
print d
print take(a,[1,0,1])

is

[3 2]
[1 2]
[1 2]
[2 3 2]




More information about the Python-list mailing list