MATLAB cells were it's attempt to handle general lists like a real language. But being MATLAB they have to be 2d. But in general, in Python uses lists where MATLAB uses cells. numpy
arrays with dtype=object
behave similarly, adding multidimensions.
Taking the object array route, I can use frompyfunc
to apply this function to elements of a list or array:
In [231]: np.frompyfunc(np.exp,1,1)([(4,2),(1,2,3)])
Out[231]:
array([array([ 54.59815003, 7.3890561 ]),
array([ 2.71828183, 7.3890561 , 20.08553692])], dtype=object)
In [232]: np.frompyfunc(np.exp,1,1)([(4,2),(1,2)])
Out[232]:
array([[54.598150033144236, 7.3890560989306504],
[2.7182818284590451, 7.3890560989306504]], dtype=object)
In the 2nd case the result is (2,2), in the first (2,) shape. That's because of how np.array([...])
handles those 2 inputs.
List comprehensions are just as fast, and probably give better control. Or at least can be more predictable.