Reputation: 15526
I have an array of function instances, and several arrays of arguments to apply to those instances. The array of function instances can be of arbitrary size. The definitions are as follows (yes, each function takes the same arguments):
fcn1(a, b, c)
fcn2(a, b, c)
Each function argument is an array of floats, for example:
a = numpy.array([1., 2., 3.])
b = numpy.array([1., 2., 3.])
c = numpy.array([1., 2., 3.])
The array of function instances looks like the following:
[<function fcn1 at 0x030A44F0> <function fcn2 at 0x030A4530>]
What I'm trying to is map the function arguments to each of the array instances.
So far I tried something simple like:
for f in fcn:
print f(a, b, c)
But the way the functions are constructed, they return X number of arrays where X is the number of elements in each of a
, b
, and c
. For example, if I simply call:
fcn1(a, b, c)
It will return (note three elements in each of the input args, three arrays returned):
[array([0.2343, 0.4943, ..., 0.8943, 0.7115]), array([0.2343, 0.4853, ..., 0.8555, 0.1915]), array([0.7773, 0.1343, ..., 0.8045, 0.9994])]
So what happens when I run my simple loop is two arrays of arrays:
[array([0.2343, 0.4943, ..., 0.8943, 0.7115]), array([0.2343, 0.4853, ..., 0.8555, 0.1915]), array([0.7773, 0.1343, ..., 0.8045, 0.9994])]
[array([0.2343, 0.4943, ..., 0.8943, 0.7115]), array([0.2343, 0.4853, ..., 0.8555, 0.1915]), array([0.7773, 0.1343, ..., 0.8045, 0.9994])]
Any thoughts on how to elegantly apply the arguments to the function instances?
Upvotes: 0
Views: 425
Reputation: 26160
If I'm reading this right, you want to end up with a data structure containing a list of lists of your function results.
function_result_set = [func(*argument_list) for func in function_list]
This will work for any arbitrary length sequence of functions (or in fact callables, such as classes) as long as each function is able to work with len(argument_list)
arguments.
Upvotes: 2
Reputation: 29740
Not sure if I'm understanding you correctly, but did you just want:
for f in fcn:
for ix in range(len(a)):
print f(a[ix], b[ix], c[ix])
Upvotes: 1