Reputation: 3373
I have set list of x and y, for example
x=[1,2,3,4,5,6]
y=[1,4,9,16,25,36]
and I need to find the y values for a set of u defined as
u = [[2,3,1],[4,2,1],[6,3,2],[2,4,5],[2,3,6]]
I am using this interpolation module. It is similar to the numpy interpolate
Currently, I follow:
v = [interp(x,y,u[i]) for i in range(len(k))]
Is there a way to circumvent the for loop, because It increases my computational time. I could rather spare my memory instead of time. My list size are very huge! I hope that inbuilt map function could be used, but not aware of how to use it.
Upvotes: 0
Views: 92
Reputation: 89087
map()
loops internally too - it just does it C-side where it is faster. However, you are already using a list comprehension, which also offloads to C and will probably be faster than map()
.
That said, there is an opportunity to gain speed and readability here by changing your loop:
v = [interp(x, y, i) for i in u]
In Python, we loop over iterators, not indices. By indirectly looping as you were, you will slow your looping down significantly. It also makes it far less readable.
As to using map()
, it could be done using functools.partial()
to make a function that calls interp()
with x
and y
prefilled:
import functools
f = functools.partial(interp, x, y)
v = list(map(f, u))
However, as I said before, this will likely be slower, and is far less readable.
(I use the term C-side in this answer presuming you are using CPython, but in any other implementation, it will also be implemented more efficiently, using whatever lower-level options are available).
Upvotes: 3