Skip to content

Instantly share code, notes, and snippets.

@bmweiner
Last active July 21, 2017 20:29
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 bmweiner/4d022eacf429de15ff477bc864d9cae1 to your computer and use it in GitHub Desktop.
Save bmweiner/4d022eacf429de15ff477bc864d9cae1 to your computer and use it in GitHub Desktop.
Common routines with numpy
from functools import reduce
import numpy as np
def mor(x):
"""x: iterable of np.array."""
return reduce(np.logical_or, x)
def mand(x):
"""x: iterable of np.array."""
return reduce(np.logical_and, x)
def madd(x):
"""Performs element-wise string concatenation with multiple input arrays.
Args:
x: iterable of np.array.
Returns: np.array.
"""
for i, arr in enumerate(x):
if type(arr.item(0)) is not str:
x[i] = x[i].astype(str)
return reduce(np.core.defchararray.add, x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment