problems with Array referencing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alcarnielo
    New Member
    • Dec 2011
    • 5

    problems with Array referencing

    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):


    Code:
    >>> from numpy import *
    >>> a = arange(10)
    >>> a
    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    >>> b = a[:5]
    >>> b
    array([0, 1, 2, 3, 4])
    >>> b[3] = 99
    >>> b
    array([ 0,  1,  2, 99,  4])
    >>> a
    array([ 0,  1,  2, 99,  4,  5,  6,  7,  8,  9])
    >>>
    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
    Last edited by zmbd; Dec 8 '12, 08:20 PM. Reason: [Z{Placed [CODE] tags}]
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    Numpy arrays are multidimensiona l 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.

    Comment

    • alcarnielo
      New Member
      • Dec 2011
      • 5

      #3
      Thaks man!!! It worked!
      Your answer also helped another friend!!!

      Comment

      Working...