简体   繁体   English

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

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

Someone shared their code and I saw a bunch of functions that were stored in what seemed to me to be a dictionary and.有人分享了他们的代码,我看到了一堆存储在我看来是字典中的函数。 So, I liked the idea and I borrowed them.所以,我喜欢这个想法并借用了它们。 The code that the person wrote it in was in JS, and I work with Python, so I translated the code into Python.那个人写的代码是用JS写的,我用的是Python,所以我把代码翻译成Python。

Here is what that person wrote in JS:这是那个人用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
}

And it works fine if you ran this code.如果您运行此代码,它就可以正常工作。 However in the code I translated, it gave me an error saying that I miss a paranthesis, comma, or a colon.但是在我翻译的代码中,它给了我一个错误,说我错过了括号、逗号或冒号。 Here is the code:这是代码:

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  
}

And what confused me is that the error was indicated to be the first key value that had an if statement in it.令我困惑的是,错误被指示为第一个包含if语句的键值。 I thought that this was allowed in Python, what is wrong with the code?我认为这在 Python 中是允许的,代码有什么问题?

As you mentioned in the comments that you still can't figure out how to do it using string keys for dictionary, I'm posting this answer.正如您在评论中提到的,您仍然无法弄清楚如何使用字典的字符串键来做到这一点,我发布了这个答案。 Though, it was partially mentioned in the comments how to do this.尽管如此,在评论中部分提到了如何做到这一点。

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))

Result:结果:

69
4761
-4623
1541364229

Again, as mentioned in comments, Python doesn't support -- operation.同样,如评论中所述,Python 不支持--操作。 Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM