Created
November 28, 2020 13:37
-
-
Save maxrohleder/57e26f34a43f074e3bcde3bb22079347 to your computer and use it in GitHub Desktop.
use array enumeration to simplify for loops
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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