There are several ways to do this you want, using Numpy, you can make use of the vectorize
:
import numpy as np
mat = np.array([[1, 2, 3],[4, 5, 6]])
soma3 = lambda x: x + 3
vectorize = np.vectorize(soma3)
print(vectorize(mat))
https://numpy.org/doc/1.17/reference/generated/numpy.vectorize.html
There are other ways to do this without using Numpy, such as creating a lambda make a for
on your list:
mat = [[1, 2, 3],[4, 5, 6]]
soma3 = lambda x: [y + 3 for y in x]
mat = list(map(soma3, mat))
print(mat)
You can also create a lambda inside of another:
mat = [[1, 2, 3],[4, 5, 6]]
soma3 = lambda x: list(map(lambda y: y + 3, x))
mat = list(map(soma3, mat))
print(mat)
And finally, you can even create a lambda to call a function from you:
def somar3(lista):
for index, item in enumerate(lista):
lista[index] = item + 3
return lista
mat = [[1, 2, 3],[4, 5, 6]]
soma3 = lambda x: somar3(x)
mat = list(map(soma3, mat))
print(mat)
In the last example, I find it "exaggerate" to use a lambda that only calls the function, can pass the direct function:
mat = list(map(somar3, mat))
:-)– hkotsubo