471,120 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

problems with Array referencing


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>
Dec 8 '12 #1
2 1706
626 Expert 512MB
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
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.

Similar topics

6 posts views Thread by Andres Rosado-Sepulveda | last post: by
2 posts views Thread by xcomm | last post: by
4 posts views Thread by Bosconian | last post: by
1 post views Thread by Picander | last post: by
4 posts views Thread by Randell D. | last post: by
7 posts views Thread by Adam Suszeck | last post: by
2 posts views Thread by Mac via DotNetMonster.com | last post: by
14 posts views Thread by julie.siebel | last post: by
6 posts views Thread by wangers16 | 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.