Skip to content

Instantly share code, notes, and snippets.

@jgoerner
Created February 8, 2019 10:21
Show Gist options
  • Save jgoerner/12c7d902dd9d37d1b5a1d1145fe16190 to your computer and use it in GitHub Desktop.
Save jgoerner/12c7d902dd9d37d1b5a1d1145fe16190 to your computer and use it in GitHub Desktop.
Get rows of 2D array that match a given other row
import numpy as np
foo = np.array([
[1, 2, 3],
[1, 2, 3],
[1, 2, 9],
])
bar = [1, 2, 3]
# Exact match
idx_all = np.apply_along_axis(
func1d=lambda x: np.equal(x, bar).all(), # use .all() for perfect match
axis=1,
arr=foo
)
print(foo[idx_all])
# OPTIONAL: partial match
idx_any = np.apply_along_axis(
func1d=lambda x: np.equal(x, bar).any(), # use .any() for partial match
axis=1,
arr=foo
)
print(foo[idx_any])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment