如何将 lambda 函数存储在 python 中的字典中?

How could I store lambda functions inside a dictionary in python?

Someone 分享了他们的代码,我看到了一堆函数,这些函数存储在我看来像是字典的东西中。所以,我喜欢这个想法,我 借用 他们。那个人写的代码是用JS写的,我用Python工作,所以我把代码运行改成了Python。

这是那个人用 JS 写的:

EasingFunctions = {
  // no easing, no acceleration
  linear: t => t,
  // accelerating from zero velocity
  easeInQuad: t => t * t,
  // decelerating to zero velocity
  easeOutQuad: t => t * (2 - t),
  // acceleration until halfway, then deceleration
  easeInOutQuad: t => t < .5 ? 2 * t * t : -1 + (4 - 2 * t) * t,
  // accelerating from zero velocity 
  easeInCubic: t => t * t * t,
  // decelerating to zero velocity 
  easeOutCubic: t => (--t) * t * t + 1,
  // acceleration until halfway, then deceleration 
  easeInOutCubic: t => t < .5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1,
  // accelerating from zero velocity 
  easeInQuart: t => t * t * t * t,
  // decelerating to zero velocity 
  easeOutQuart: t => 1 - (--t) * t * t * t,
  // acceleration until halfway, then deceleration
  easeInOutQuart: t => t < .5 ? 8 * t * t * t * t : 1 - 8 * (--t) * t * t * t,
  // accelerating from zero velocity
  easeInQuint: t => t * t * t * t * t,
  // decelerating to zero velocity
  easeOutQuint: t => 1 + (--t) * t * t * t * t,
  // acceleration until halfway, then deceleration 
  easeInOutQuint: t => t < .5 ? 16 * t * t * t * t * t : 1 + 16 * (--t) * t * t * t * t
}

如果您 运行 此代码,它工作正常。但是在我 t运行 指定的代码中,它给了我一个错误,说我错过了 pa运行thesis、逗号或冒号。这是代码:

EasingFunctions = {
  # no easing, no acceleration
  linear: lambda t : t,
  # accelerating from zero velocity
  easeInQuad: lambda t : t ** 2,
  # decelerating to zero velocity
  easeOutQuad: lambda t : t * (2-t),
  # acceleration until halfway, then deceleration
  easeInOutQuad: (lambda t : t = (2*(t**2)) if t < 0.5 else ((-1+(4-2*t)) * t)),
  # accelerating from zero velocity 
  easeInCubic: lambda t : t * t * t,
  # decelerating to zero velocity 
  easeOutCubic: lambda t : (t-1) * t * t + 1, 
  # acceleration until halfway, then deceleration 
  easeInOutCubic: lambda t : t = 4*t*t*t if t < 0.5 else (t - 1) * (2 * t - 2) * (2 * t - 2) + 1,
  # accelerating from zero velocity 
  easeInQuart: lambda t : t ** 4,
  # decelerating to zero velocity 
  easeOutQuart: lambda t : 1 - (t-1) * t * t * t,
  # acceleration until halfway, then deceleration
  easeInOutQuart: lambda t : t = 8 * t * t * t * t if t < 0.5 else 1 - 8 * (t) * t * t * t
  # accelerating from zero velocity
  easeInQuint: lambda t : t ** 5,
  # decelerating to zero velocity
  easeOutQuint: lambda t : 1 + (t-1) * t * t * t * t,
  # acceleration until halfway, then deceleration 
  easeInOutQuint: lambda t : t = 16 * t * t * t * t * t if t < 0.5 else 1 + 16 * (t-1) * t * t * t * t  
}

让我感到困惑的是,该错误被指示为第一个包含 if 语句的键值。我认为这在 Python 中是允许的,代码有什么问题?

正如您在评论中提到的,您仍然无法弄清楚如何使用字典的字符串键来做到这一点,我发布了这个答案。不过,在评论中部分提到了如何做到这一点。

a = {
    'linear': lambda t: t,
    'easeInQuad': lambda t: t ** 2,
    'easeOutQuad': lambda t: t * (2-t),
    'easeOutQuint': lambda t: 1 + (t - 1) * t * t * t * t,
}

print(a['linear'](69))
print(a['easeInQuad'](69))
print(a['easeOutQuad'](69))
print(a['easeOutQuint'](69))

结果:

69
4761
-4623
1541364229

同样,如评论中所述,Python 不支持 -- 操作。希望这有帮助。