In this python numpy program, we will copy data from a given array to another array.
Steps to solve the program
- Import the numpy library as np.
- Create an array using np.array().
- Create an empty array of the same size as the given array.
- Use for loop to iterate over the given array and add all elements to the empty array.
- Print both arrays to see the output.
import numpy as np
x = np.array([[6, 5, 7],[10, 9, 1],[7, 8, 2]])
y = [None] * len(x)
print(y)
for i in range(0,len(x)):
y[i] = x[i]
print("Elements of original array: ");
for i in range(0, len(x)):
print(x[i])
print("Elements of new array: ");
for i in range(0, len(y)):
print(y[i])
Output :
[None, None, None]
Elements of original array:
[6 5 7]
[10 9 1]
[7 8 2]
Elements of new array:
[6 5 7]
[10 9 1]
[7 8 2]
calculate the sum of all columns of an array