Skip to content

Instantly share code, notes, and snippets.

@maxrohleder
Last active July 23, 2021 06:58
Show Gist options
  • Save maxrohleder/3decf02f8edc48c663aa78209b514554 to your computer and use it in GitHub Desktop.
Save maxrohleder/3decf02f8edc48c663aa78209b514554 to your computer and use it in GitHub Desktop.
smart indexing can save you a lot of looping
a = np.array([[0, 1, 2],
[3, 4, 5]])
b = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# subsetting of the array (start is included, end is excluded)
print(a[0:2, 0:2])
#> [[0 1]
#> [3 4]]
# striding / stepping "every second entry"
print(b[::2])
#> [0 2 4 6 8]
# indexing from back to front
print(b[-1], b[-3])
#> 9 7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment