Skip to content

Instantly share code, notes, and snippets.

@jmmshn
Created April 15, 2020 22:56
Show Gist options
  • Save jmmshn/818b3e419b85a1e1a4f905800a7f8061 to your computer and use it in GitHub Desktop.
Save jmmshn/818b3e419b85a1e1a4f905800a7f8061 to your computer and use it in GitHub Desktop.
[Get Filtered Positions] Take an array and a filtering function return all the positions and values for which taht function is true
from itertools import product
def get_filtered_pos(arr, func):
"""
Take any ndarray and return the position
"""
d_ranges = [range(_) for _ in arr.shape]
for j in product(*d_ranges):
if func(arr[j]):
yield j, arr[j]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment