How to Check If Original NumPy Array Changes When View Changes


In this example we will show how to create a view and check if the original NumPy array changes when the view changes in Python.

Source Code

import numpy as np

n = np.array([1, 2, 3, 4])
view_n = n.view()
print(view_n)

view_n[2] = 10

# any changes made to the view will affect the original array
print(n)
print(view_n)

Output:

[1 2 3 4]
[ 1  2 10  4]
[ 1  2 10  4]
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments