私信  •  关注

Jean-Baptiste Yunès

Jean-Baptiste Yunès 最近创建的主题
Jean-Baptiste Yunès 最近回复了
6 年前
回复了 Jean-Baptiste Yunès 创建的主题 » 如何在python函数中添加f(x)作为参数?

使用这个:

def maxf(function,interval):
  maxresult = 0
  for x in range(interval[0]-1,interval[1]+1):
    result=float(function(x))
    if result >= maxresult:
      maxresult = result
      maxresultx = x

  return maxresult,maxresultx
print(maxf(lambda x: x**2,[1,3]))

lambda 定义作为参数传递的函数(匿名函数),因此 maxf 可以根据需要调用它。