[Matrix-SIG] array assignments
Janko Hauser
jhauser@ifm.uni-kiel.de
Thu, 7 Oct 1999 13:04:23 +0200 (CEST)
Pedro Miguel Frazao Fernandes Ferreira writes:
> Hi All,
>
> Can anyone tell me if this is the correct behaviour:
>
> Python 1.5.1 (#1, Dec 17 1998, 20:58:15) [GCC 2.7.2.3] on linux2
> Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
> Executing startup script
> End
> >>> a=arange(10)
> >>> b=arange(10)
> >>> a=b
> >>> b[5]=10
> >>> print a
> [ 0 1 2 3 4 10 6 7 8 9]
> >>>
>
> Should a and b be two different objects ?
> = operator should copy ?
>
No and no.
>>> a=arange(10)
>>> b=a
>>> b[5]=10
>>> a
array([ 0, 1, 2, 3, 4, 10, 6, 7, 8, 9])
>>> b
array([ 0, 1, 2, 3, 4, 10, 6, 7, 8, 9])
>>> b=a*1. # or copy.copy(a)
>>> b[5]=5
>>> a
array([ 0, 1, 2, 3, 4, 10, 6, 7, 8, 9])
>>> b
array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
>>> b=array(a,copy=1)
>>> b[5]=5
>>> a
array([ 0, 1, 2, 3, 4, 10, 6, 7, 8, 9])
>>> b
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
The following is special for NumPy arrays not like the behaviour of
python lists.
>>> b=a[:]
>>> b[5]=10
>>> a
array([ 0, 1, 2, 3, 4, 10, 6, 7, 8, 9])
>>> b
array([ 0, 1, 2, 3, 4, 10, 6, 7, 8, 9])
HTH
__Janko