python - 动态创建 lambda 函数

标签 python numpy lambda

有什么方法可以动态创建 lambda 函数吗?例如,

f = lambda t : (1 + 32*t + 23*np.power(t,2) + 23*np.power(t,3) + 23*np.power(t,4))

在上面的 lambda 函数 f 中,我想在 23*np.power(t,2) 中增加 t 的值并希望它一直持续到 467。所以:

f = lambda t : (1 + 32*t + 23*np.power(t,2) + .. + 23*np.power(t,467))

有没有办法让我自动执行此操作?

最佳答案

你可能会喜欢 f = lambda t : 1 + sum(23 * np.power(t, i) for i in range(1, 467))。您不能使用 for 循环或类似的东西,但可以使用简单的表达式、函数调用等。

关于python - 动态创建 lambda 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56313362/

相关文章:

python - Pandas 中数据框行中的数据框列

python - 如何在 python pandas 中使用 str.contains 拆分具有多个类别的数据帧?

Python 重复 : Is there a conditional/lambda based repeat method for python?

numpy - 为什么ones_like被列为ufunc?

c++ - 在 lambda 中捕获参数包时,一些整数会丢失它们的值

linq - 使用 LINQ 的魔力 - 如何为每个匹配的条件调用委托(delegate)?

python - pandas-两列的值是列表,需要生成一个新的 df 来显示它们之间的所有值组合

python - 如何在 Python 中混合使用多个可选参数?

Python:枚举值与命名单元

Python NumPy vs Octave/MATLAB 精度