• <small id='qlIdZ'></small><noframes id='qlIdZ'>

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

        Python 中的列表理解和 lambda

        时间:2024-04-20

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

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

              <tbody id='Yuppt'></tbody>

            <legend id='Yuppt'><style id='Yuppt'><dir id='Yuppt'><q id='Yuppt'></q></dir></style></legend>
            <tfoot id='Yuppt'></tfoot>
              <bdo id='Yuppt'></bdo><ul id='Yuppt'></ul>

                  本文介绍了Python 中的列表理解和 lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想创建一个 lambda 列表,但结果并不如我所愿.

                  I wanted to create a list of lambdas, but it didn't quite work out as I hoped.

                  L = [(lambda x: x/y) for y in range(10)]
                  

                  我希望列表中的每个函数都将其参数除以其索引,但所有函数仅除以最后一个索引.

                  I expected every function in the list to divide its argument by its index, but all functions only divide by the last index.

                  >>> L[1](5)
                  0.5555555555555556
                  >>> L[5](5)
                  0.5555555555555556
                  >>> 5/9
                  0.5555555555555556
                  

                  这种列表推导式(每个 lambda 都有自己的 y副本)在 Python 中是否可行?

                  Is this kind of list comprehension, where every lambda has its own copy of ypossible in Python?

                  推荐答案

                  你的 lambda 中的 y 指的是 y 在它来自的范围内的最后一个值,即 9.

                  The y in your lambda refers to the last value that y had in the scope it came from, i.e., 9.

                  获得所需行为的最简单方法是在 lambda 中使用默认参数:

                  The easiest way to get the behavior you want is to use a default argument in your lambda:

                  lambda x, y=y: x/y
                  

                  这会在定义 lambda 函数时捕获 y 的值.

                  This captures the value of y at the moment the lambda function is defined.

                  你也可以做一个double-lambda",调用一个返回你想要的lambda的函数,传入y的期望值:

                  You can also do a "double-lambda", calling a function that returns the lambda you want, passing in the desired value of y:

                  (lambda y: lambda x: x/y)(y)
                  

                  这里,每次调用外部 lambda 时都会提供一个新范围.

                  Here, the outer lambda provides a new scope each time you call it.

                  这篇关于Python 中的列表理解和 lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:输入中的 print 语句返回“none" 下一篇:如何将 24 小时时间转换为 12 小时时间?

                  相关文章

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

                  <legend id='1oVvh'><style id='1oVvh'><dir id='1oVvh'><q id='1oVvh'></q></dir></style></legend>

                    <tfoot id='1oVvh'></tfoot>
                  1. <small id='1oVvh'></small><noframes id='1oVvh'>

                      <bdo id='1oVvh'></bdo><ul id='1oVvh'></ul>