Created
February 8, 2019 10:21
-
-
Save jgoerner/12c7d902dd9d37d1b5a1d1145fe16190 to your computer and use it in GitHub Desktop.
Get rows of 2D array that match a given other row
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 | |
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