From 3e20aa0fcbff978e917eeb8ced25028291d4ae0f Mon Sep 17 00:00:00 2001 From: David Rotermund <54365609+davrot@users.noreply.github.com> Date: Thu, 14 Dec 2023 18:01:37 +0100 Subject: [PATCH] Update README.md Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com> --- numpy/advanced_indexing/README.md | 33 +++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/numpy/advanced_indexing/README.md b/numpy/advanced_indexing/README.md index 26cdf22..91c6eec 100644 --- a/numpy/advanced_indexing/README.md +++ b/numpy/advanced_indexing/README.md @@ -77,3 +77,36 @@ Output: [1 1 1]] ``` +## Index vs Slices / Views + +This procedure is called indexing: + +```python +import numpy as np + +a = np.arange(0, 10) +idx = np.arange(2,5) +b = a[idx] + +print(idx) # -> [2 3 4] +print() +print(b) # -> [2 3 4] +print() +print(np.may_share_memory(a,b)) # -> False +``` + +While this is called slicing: + +```python +import numpy as np + +a = np.arange(0, 10) +b = a[2:5] + +print(b) # -> [2 3 4] +print() +print(np.may_share_memory(a, b)) # -> True +``` + +As you can see lies the biggest different in the creation of a view when we use slicing. Indexing creates a new object instead. +