<legend id='eba2E'><style id='eba2E'><dir id='eba2E'><q id='eba2E'></q></dir></style></legend>
    <tfoot id='eba2E'></tfoot>

    <i id='eba2E'><tr id='eba2E'><dt id='eba2E'><q id='eba2E'><span id='eba2E'><b id='eba2E'><form id='eba2E'><ins id='eba2E'></ins><ul id='eba2E'></ul><sub id='eba2E'></sub></form><legend id='eba2E'></legend><bdo id='eba2E'><pre id='eba2E'><center id='eba2E'></center></pre></bdo></b><th id='eba2E'></th></span></q></dt></tr></i><div id='eba2E'><tfoot id='eba2E'></tfoot><dl id='eba2E'><fieldset id='eba2E'></fieldset></dl></div>
  • <small id='eba2E'></small><noframes id='eba2E'>

      • <bdo id='eba2E'></bdo><ul id='eba2E'></ul>

      1. 什么是 memoization 以及如何在 Python 中使用它?

        What is memoization and how can I use it in Python?(什么是 memoization 以及如何在 Python 中使用它?)
          <tbody id='fXXBZ'></tbody>

            <i id='fXXBZ'><tr id='fXXBZ'><dt id='fXXBZ'><q id='fXXBZ'><span id='fXXBZ'><b id='fXXBZ'><form id='fXXBZ'><ins id='fXXBZ'></ins><ul id='fXXBZ'></ul><sub id='fXXBZ'></sub></form><legend id='fXXBZ'></legend><bdo id='fXXBZ'><pre id='fXXBZ'><center id='fXXBZ'></center></pre></bdo></b><th id='fXXBZ'></th></span></q></dt></tr></i><div id='fXXBZ'><tfoot id='fXXBZ'></tfoot><dl id='fXXBZ'><fieldset id='fXXBZ'></fieldset></dl></div>

            <small id='fXXBZ'></small><noframes id='fXXBZ'>

            <tfoot id='fXXBZ'></tfoot>
              <bdo id='fXXBZ'></bdo><ul id='fXXBZ'></ul>
              1. <legend id='fXXBZ'><style id='fXXBZ'><dir id='fXXBZ'><q id='fXXBZ'></q></dir></style></legend>
                  本文介绍了什么是 memoization 以及如何在 Python 中使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我刚开始使用 Python,我不知道 memoization 是什么以及如何使用它.另外,我可以举一个简化的例子吗?

                  I just started Python and I've got no idea what memoization is and how to use it. Also, may I have a simplified example?

                  推荐答案

                  记忆化是指根据方法输入记忆(记忆化"→备忘录"→被记忆)方法调用的结果,然后返回记忆的结果而不是再次计算结果.您可以将其视为方法结果的缓存.有关详细信息,请参阅第 387 页以了解 Introduction To Algorithms (3e), Cormen 等人中的定义.

                  Memoization effectively refers to remembering ("memoization" → "memorandum" → to be remembered) results of method calls based on the method inputs and then returning the remembered result rather than computing the result again. You can think of it as a cache for method results. For further details, see page 387 for the definition in Introduction To Algorithms (3e), Cormen et al.

                  在 Python 中使用 memoization 计算阶乘的简单示例如下所示:

                  A simple example for computing factorials using memoization in Python would be something like this:

                  factorial_memo = {}
                  def factorial(k):
                      if k < 2: return 1
                      if k not in factorial_memo:
                          factorial_memo[k] = k * factorial(k-1)
                      return factorial_memo[k]
                  

                  你可以再复杂一点,把memoization过程封装成一个类:

                  You can get more complicated and encapsulate the memoization process into a class:

                  class Memoize:
                      def __init__(self, f):
                          self.f = f
                          self.memo = {}
                      def __call__(self, *args):
                          if not args in self.memo:
                              self.memo[args] = self.f(*args)
                          #Warning: You may wish to do a deepcopy here if returning objects
                          return self.memo[args]
                  

                  然后:

                  def factorial(k):
                      if k < 2: return 1
                      return k * factorial(k - 1)
                  
                  factorial = Memoize(factorial)
                  

                  在 Python 2.4 中添加了一个名为decorators"的功能现在允许您简单地编写以下代码来完成相同的事情:

                  A feature known as "decorators" was added in Python 2.4 which allow you to now simply write the following to accomplish the same thing:

                  @Memoize
                  def factorial(k):
                      if k < 2: return 1
                      return k * factorial(k - 1)
                  

                  Python 装饰器库 有一个类似的装饰器,称为 memoized 比此处显示的 Memoize 类更健壮.

                  The Python Decorator Library has a similar decorator called memoized that is slightly more robust than the Memoize class shown here.

                  这篇关于什么是 memoization 以及如何在 Python 中使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)

                  <small id='KJ6Ec'></small><noframes id='KJ6Ec'>

                    • <bdo id='KJ6Ec'></bdo><ul id='KJ6Ec'></ul>
                    • <tfoot id='KJ6Ec'></tfoot>
                        <tbody id='KJ6Ec'></tbody>
                        <legend id='KJ6Ec'><style id='KJ6Ec'><dir id='KJ6Ec'><q id='KJ6Ec'></q></dir></style></legend>

                          • <i id='KJ6Ec'><tr id='KJ6Ec'><dt id='KJ6Ec'><q id='KJ6Ec'><span id='KJ6Ec'><b id='KJ6Ec'><form id='KJ6Ec'><ins id='KJ6Ec'></ins><ul id='KJ6Ec'></ul><sub id='KJ6Ec'></sub></form><legend id='KJ6Ec'></legend><bdo id='KJ6Ec'><pre id='KJ6Ec'><center id='KJ6Ec'></center></pre></bdo></b><th id='KJ6Ec'></th></span></q></dt></tr></i><div id='KJ6Ec'><tfoot id='KJ6Ec'></tfoot><dl id='KJ6Ec'><fieldset id='KJ6Ec'></fieldset></dl></div>