Skip to content

Instantly share code, notes, and snippets.

@yzhong52
Created September 2, 2019 12:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yzhong52/3f4765d2d82fdfbeb06058bac41cfedf to your computer and use it in GitHub Desktop.
Save yzhong52/3f4765d2d82fdfbeb06058bac41cfedf to your computer and use it in GitHub Desktop.
Differences between Numpy array and regular python array

In regular python array, a slice view of an array is actually a copy. Modifiying elements through the slice won't affect the original array.

>>> arr = [0, 1, 2]
>>> arr[0:][0] = 100
>>> arr
[0, 1, 2]

However, with numpy, the slice view of the array won't create a extra copy. And thus, modifying the elements through the slice will change the original data.

>>> import numpy as np
>>> arr = np.array([0, 1, 2])
>>> arr[0:][0] = 100
>>> arr
array([100,   1,   2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment