Skip to content

Instantly share code, notes, and snippets.

@Keiku
Created August 5, 2021 05:17
Show Gist options
  • Save Keiku/9fa4a944128990d931f5ff1592af7723 to your computer and use it in GitHub Desktop.
Save Keiku/9fa4a944128990d931f5ff1592af7723 to your computer and use it in GitHub Desktop.
Get the index that meets the conditions from the 2d array
import numpy as np
# sample array
array = np.array([[1, 1, 1, 0, 0, 0],
[1, 1, 1, 0, 0, 0]])
# get row index and col index
rows, cols = np.where(array == 1)
# Convert row index and column index to 2d array
np.transpose((rows, cols))
# array([[0, 0],
# [0, 1],
# [0, 2],
# [1, 0],
# [1, 1],
# [1, 2]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment