Numeric Arrays
There are some important differences between Numeric arrays and built-in Python lists. In particular, Numeric arrays always hold pointers to the data, not actual copies of the data. As a result, taking a slice of a Numeric array does not copy the underlying data, it only returns an array of pointers to the same data. An example of this is shown below.
>>> import Numeric >>> x = Numeric.zeros(10, 'i') >>> x array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0],'i') >>> y = x[:] >>> y array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0],'i') >>> x[0] = 1 >>> y array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0],'i')
In the above example, note that when slicing is used, the underlying data is not copied, reflected by that fact that when x[0] changed, y[0] changed also. In order to avoid this, one can use the copy method from the built-in copy module. This function copies the underlying data instead of just the pointers. An example of using the copy method is shown below. Notice that the values of x[0] and y[0] are now independent of each other.
>>> import Numeric >>> import copy >>> x = Numeric.zeros(10, 'i') >>> x array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0],'i') >>> y = copy.copy(x) >>> x[0]=1 >>> y array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0],'i')
