471,344 Members | 1,499 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,344 software developers and data experts.

copy a numpy array

hi,
(i posted this to numpy discussion grp couple of days back ..but it
fails to appear..)since it is needed for my work i would appreciate if
anyone can help me with this question
i have two ndarrays of 1000 elements each and want to copy all
elements from srcarray to destarray

srcarray=numpy.array( [3973334.8381791776,........,382999.6748692277] )

arrsize=1000
destarray=zeros(arrsize)

i copied all items from src to dest by using
destarray[0:arrsize]=srcarray[0:arrsize]

i don't know if this is the right way to do the copying.
is there a better(efficient?) way ?
jim

Jan 8 '08 #1
4 12697
On Jan 8, 2008 4:32 PM, jimgardener <ji*********@gmail.comwrote:
hi,
(i posted this to numpy discussion grp couple of days back ..but it
fails to appear..)since it is needed for my work i would appreciate if
anyone can help me with this question
i have two ndarrays of 1000 elements each and want to copy all
elements from srcarray to destarray

srcarray=numpy.array( [3973334.8381791776,........,382999.6748692277] )

arrsize=1000
destarray=zeros(arrsize)

i copied all items from src to dest by using
destarray[0:arrsize]=srcarray[0:arrsize]

i don't know if this is the right way to do the copying.
is there a better(efficient?) way ?
jim

If you want the array to share the data, just use
destarray = numpy.array(srcarray)

Otherwise you can set the copy flag to False:

destarray = numpy.array(srcarray,copy=False)

bye,
Francesco
Jan 8 '08 #2
I am new to python and everything related to it, and it so happens
that I just went through the numpy tutorial last night, it is in

http://www.scipy.org/Tentative_NumPy_Tutorial

and the answer to your question is in section 3.7

Basically, if you want to make a (deep) copy of it:

destarray = srcarray.copy()

gsal
Jan 8 '08 #3
thanx guys for the replies
need a little clarification

srcarray=array([1.2,2.3,3.4,4.5,5.6])
destarray=array(srcarray,copy=False)

then
srcarray[2]=99.9
will cause the change to be reflected in both src and dest.
doesn't that mean data is shared between both arrays?

but if i do
destarray=array(srcarray)
or
destarray=srcarray.copy()
then the change in one array will not affect the other,meaning no data
sharing
....want to know if my understanding is right
jim
Jan 9 '08 #4
On Jan 9, 2008 6:35 AM, jimgardener <ji*********@gmail.comwrote:
thanx guys for the replies
need a little clarification

srcarray=array([1.2,2.3,3.4,4.5,5.6])
destarray=array(srcarray,copy=False)

then
srcarray[2]=99.9
will cause the change to be reflected in both src and dest.
doesn't that mean data is shared between both arrays?

but if i do
destarray=array(srcarray)
or
destarray=srcarray.copy()
then the change in one array will not affect the other,meaning no data
sharing
...want to know if my understanding is right
jim
You're right, I wrote too quickly and so I gave a wront information! sorry! :-)

Just try the following:
Apart from the copy method (which other have correctly pointed to),
the correct version is simply:

a = numpy.array([1,2])
b = numpy.array(a)

and now try modifying one of them.

bye
Francesco
Jan 9 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by K. Jansma | last post: by
2 posts views Thread by robert | last post: by
2 posts views Thread by Chris Smith | last post: by
3 posts views Thread by Duncan Smith | last post: by
2 posts views Thread by Rick Giuly | last post: by
3 posts views Thread by Rüdiger Werner | last post: by
reply views Thread by Ronak mishra | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.