0

I'm trying to achieve something of this sort:

f1 = lambda x: x**2
f2 = lambda x: x**3
fn = np.array([f1, f2])
print(fn(3))

So that it returns an array with the outputs in place. Of course, I could do it like this:

result = np.array([f(x) for f in fn])

But this seems like an inefficient way to do it... especially if there are a lot of such functions.

Is there an alternative to this? Something more efficient; maybe like numpy ufuncs? I do not know how to do the implementation.

amzon-ex
  • 1,645
  • 1
  • 6
  • 28
  • 1
    The fast efficient numpy code works primarily with numeric dtypes. Your `fn` is object dtype. There is little, if any, advantage to using such an array instead of a list. – hpaulj Apr 24 '20 at 18:29
  • @hpaulj So, I should just stick to the way I'm doing it, or could there be a better method? – amzon-ex May 13 '20 at 08:28

0 Answers0