Skip to content

Instantly share code, notes, and snippets.

@pyrtsa
Created February 28, 2010 23:27
Show Gist options
  • Save pyrtsa/317912 to your computer and use it in GitHub Desktop.
Save pyrtsa/317912 to your computer and use it in GitHub Desktop.
def combine(function, *arrays)
def combine(function, *arrays):
"""Construct an ndarray through a cartesian product of N sequences.
Example:
>>> combine(sum, [1,3], [.1,.2,.3], [0,10,100])
array([[[ 1.1, 11.1, 101.1],
[ 1.2, 11.2, 101.2],
[ 1.3, 11.3, 101.3]],
<BLANKLINE>
[[ 3.1, 13.1, 103.1],
[ 3.2, 13.2, 103.2],
[ 3.3, 13.3, 103.3]]])
"""
from itertools import product
from numpy import reshape
return reshape(map(function, product(*arrays)), map(len, arrays))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment