By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,746 Members | 1,208 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,746 IT Pros & Developers. It's quick & easy.

problems with Array referencing

P: 5
Hello,

I'm having some problems with python 2.6 and I fount the same problem with python 3.2.2

I've never noted that before until today.

When I referenciate an array to a var and after that referenciate part of this array to another var and finely change an item of this second var, the first var have the item changed also. For instance (this is a simple exemple of shell):


Expand|Select|Wrap|Line Numbers
  1. >>> from numpy import *
  2. >>> a = arange(10)
  3. >>> a
  4. array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
  5. >>> b = a[:5]
  6. >>> b
  7. array([0, 1, 2, 3, 4])
  8. >>> b[3] = 99
  9. >>> b
  10. array([ 0,  1,  2, 99,  4])
  11. >>> a
  12. array([ 0,  1,  2, 99,  4,  5,  6,  7,  8,  9])
  13. >>> 
I thought that only one item should change (the var b[3]), not also the array that I first created (a[3]).

I've also tried with the traditional list, and the same thing have been verified in python 26.

Is that a problem with the language logic or is that ok?
For me it's nog a good thing, because I have to control the variable progress and it won help me in that way.

2011/12/10 Bytes <support@bytes.com>
28582038
Dec 8 '12 #1
Share this Question
Share on Google+
2 Replies

Expert 100+
P: 626
Numpy arrays are multidimensional even though you only defined one dimension. Use deepcopy to copy compound objects as it does not use references.
A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.

A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.
Dec 8 '12 #2

P: 5
Thaks man!!! It worked!
Your answer also helped another friend!!!
Dec 9 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.