For loops and list comprehensions are slow in Numpy so they are avoided. However, I have a tuple of 2d Numpy arrays with varying number of columns so that it cannot be converted into a 3d array for manipulation by slicing. Is it possible to access the elements in the tuple without a for loop or list comprehension?
Example problem showing my list comprehension approach (note myfunc may not be trivial as shown as in this case)
def myfunc(in_array):
return sum(in_array)
a1 = np.array(
[[1, 2],
[3, 4]]
)
a2 = np.array(
[[5, 6],
[7, 8],
[9, 10]]
)
data = (a1, a2)
ans = [myfunc(a) for a in data]
print(ans)
Output:
[array([4, 6]), array([21, 24])]