Created
February 28, 2010 23:27
-
-
Save pyrtsa/317912 to your computer and use it in GitHub Desktop.
def combine(function, *arrays)
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
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