In this Page, We are Providing Python Programming – Copying Array. Students can visit for more Detail and Explanation of Python Handwritten Notes Pdf.
Python Programming – Copying Array
Copying array
When operating and manipulating arrays, their elements sometimes needs to be copied into a new array, and sometimes not. This is often a source of confusion for beginners. Simple assignment does not make copy of array data.
>>> a=np . arange ( 12 ) >>> b=a >>> id ( a ) 85660040 >>> id ( b ) 85660040 >>> b is a # a and b are two names for the same ndarray object True
Python passes mutable objects as references, so function calls make no copy.
>>> def F ( x ) : . . . print id ( x ) . . . >>> id ( a ) 85660040 >>> F ( a ) 85660040
The copy method makes a complete copy of the array data.
>>> c=a . copy ( ) >>> c array ( [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 ] ) >>> c is a False >>> c [ 0 ]=999 >>> c array ( [ 999 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 ] ) >>> a array ( [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 ] )