Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Appending values at the end of an NumPy array
Next article icon

Many times there is a need to copy one array to another. Numpy provides the facility to copy array using different methods. In this

Copy NumPy Array into Another Array

There are various ways to copies created inNumPyarrays inPython, here we are discussing some generally used methods for copies created inNumPy arrays those are following.

Copy 1D Numpy Arrays into Another Usingnp.copy() Function

In the below example, the given Numpy array 'org_array' is copied to another array 'copy_array' using np.copy () function.

Python3
# importing Numpy packageimportnumpyasnp# Creating a numpy array using np.array()org_array=np.array([1.54,2.99,3.42,4.87,6.94,8.21,7.65,10.50,77.5])print("Original array: ")# printing the Numpy arrayprint(org_array)# Now copying the org_array to copy_array# using np.copy() functioncopy_array=np.copy(org_array)print("\nCopied array: ")# printing the copied Numpy arrayprint(copy_array)

Output:

Original array:
[ 1.54 2.99 3.42 4.87 6.94 8.21 7.65 10.5 77.5 ]
Copied array:
[ 1.54 2.99 3.42 4.87 6.94 8.21 7.65 10.5 77.5 ]

Copy Given 2-D Array to Another Array Usingnp.copy() Function

In this example, the given 2-D Numpy array 'org_array' is copied to another array 'copy_array' using np.copy () function.

Python3
# importing Numpy packageimportnumpyasnp# Creating a 2-D numpy array using np.array()org_array=np.array([[23,46,85],[43,56,99],[11,34,55]])print("Original array: ")# printing the Numpy arrayprint(org_array)# Now copying the org_array to copy_array# using np.copy() functioncopy_array=np.copy(org_array)print("\nCopied array: ")# printing the copied Numpy arrayprint(copy_array)

Output:

Original array:
[[23 46 85]
[43 56 99]
[11 34 55]]
Copied array:
[[23 46 85]
[43 56 99]
[11 34 55]]

NumPy Array Copy UsingAssignment Operator

In the below example, the given Numpy array 'org_array' is copied to another array 'copy_array' using Assignment Operator.

Python3
# importing Numpy packageimportnumpyasnp# Create a 2-D Numpy array using np.array()org_array=np.array([[99,22,33],[44,77,66]])# Copying org_array to copy_array# using Assignment operatorcopy_array=org_array# modifying org_arrayorg_array[1,2]=13# checking if copy_array has remained the same# printing original arrayprint('Original Array:\n',org_array)# printing copied arrayprint('\nCopied Array:\n',copy_array)

Output:

Original Array:
[[99 22 33]
[44 77 13]]
Copied Array:
[[99 22 33]
[44 77 13]]

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp