Skip to content

Instantly share code, notes, and snippets.

@mstankie
Last active September 5, 2022 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mstankie/1fe659a89d87fefe1149a4a2460f2165 to your computer and use it in GitHub Desktop.
Save mstankie/1fe659a89d87fefe1149a4a2460f2165 to your computer and use it in GitHub Desktop.
List of 3D grid indices in NumPy
# Get i, j, k indices of grid nodes as a list to iterate over
import numpy as np
GRID_SHAPE = (2, 3, 4)
grid_indices: np.ndarray = np.indices(GRID_SHAPE).reshape(len(GRID_SHAPE), -1).T
grid_indices.shape # >> (24, 3)
# This way we can loop through the indices as array rows
for idx in grid_indices:
print(idx)
grid_indices_list_of_tuples: list[tuple[int, int, int]] = list(map(tuple, grid_indices))
# This way we can loop through the indices as list of tuples
for idx in grid_indices_list_of_tuples:
print(idx)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment