Skip to content

Instantly share code, notes, and snippets.

@maxrohleder
Created November 28, 2020 13:37
Show Gist options
  • Save maxrohleder/57e26f34a43f074e3bcde3bb22079347 to your computer and use it in GitHub Desktop.
Save maxrohleder/57e26f34a43f074e3bcde3bb22079347 to your computer and use it in GitHub Desktop.
use array enumeration to simplify for loops
import numpy as np
img = np.arange(6).reshape(2, 3)
# looping over all elements like this ...
for x in range(img.shape[0]):
for y in range(img.shape[1]):
print(img[x, y])
# ... is easier and more readable with numpy
for (x, y), value in np.ndenumerate(img):
print(value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment