如何在python中的第二个数组对应的数组中找到一个元素?

How to find an element in an array that corresponds to the second array in python?

我有两个一维数组:

x  y
0  5
2  10
4  15
6  20
8  25
10 30

我想在给定 x 处找到 y 的值。但是元素不在数组中,而是元素的值在这些元素之间。比如我想在x = 2.5或者x = 5的时候求y的值。我可能应该使用插值函数,但我发现它很混乱。

要在值之间进行插值,我建议使用 scipy.interpolate.interp1d

from scipy.interpolate import interp1d
f = interp1d(x, y)

这将创建一个函数 f,您可以向其中传递值,它将 return 内插值。即 f(3) 将输出 12.5.