<legend id='gQq55'><style id='gQq55'><dir id='gQq55'><q id='gQq55'></q></dir></style></legend>

        <bdo id='gQq55'></bdo><ul id='gQq55'></ul>

      <tfoot id='gQq55'></tfoot>

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

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

        python中是否有没有结果的地图?

        时间:2023-10-19
        <legend id='4po7S'><style id='4po7S'><dir id='4po7S'><q id='4po7S'></q></dir></style></legend>

        <small id='4po7S'></small><noframes id='4po7S'>

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

                <tbody id='4po7S'></tbody>
              <tfoot id='4po7S'></tfoot>

                • 本文介绍了python中是否有没有结果的地图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  有时,我只想为条目列表执行一个函数——例如:

                  Sometimes, I just want to execute a function for a list of entries -- eg.:

                  for x in wowList:
                     installWow(x, 'installed by me')
                  

                  有时我需要这些东西来进行模块初始化,所以我不想在全局命名空间中有像 x 这样的足迹.一种解决方案是将 map 与 lambda 一起使用:

                  Sometimes I need this stuff for module initialization, so I don't want to have a footprint like x in global namespace. One solution would be to just use map together with lambda:

                  map(lambda x: installWow(x, 'installed by me'), wowList)
                  

                  但这当然会创建一个不错的列表 [None, None, ...] 所以我的问题是,如果有没有返回列表的类似函数 - 因为我只是不需要它.

                  But this of course creates a nice list [None, None, ...] so my question is, if there is a similar function without a return-list -- since I just don't need it.

                  (当然我也可以使用 _x,因此不会留下可见的足迹——但地图解决方案看起来很整洁......)

                  (off course I can also use _x and thus not leaving visible footprint -- but the map-solution looks so neat ...)

                  推荐答案

                  你可以使用内置的any函数将一个函数不带return语句应用于任意项由生成器返回而不创建列表.可以这样实现:

                  You can use the built-in any function to apply a function without return statement to any item returned by a generator without creating a list. This can be achieved like this:

                  any(installWow(x, 'installed by me') for x in wowList)
                  

                  我发现这是您想要实现的最简洁的习惯用法.

                  I found this the most concise idom for what you want to achieve.

                  在内部,installWow 函数确实返回 None,它在逻辑操作中的计算结果为 False.any 基本上将 or 归约操作应用于生成器返回的所有项目,当然都是 None,所以它必须遍历所有生成器返回的项目.最后它确实返回 False,但这并不需要打扰你.好消息是:没有创建列表作为副作用.

                  Internally, the installWow function does return None which evaluates to False in logical operations. any basically applies an or reduction operation to all items returned by the generator, which are all None of course, so it has to iterate over all items returned by the generator. In the end it does return False, but that doesn't need to bother you. The good thing is: no list is created as a side-effect.

                  请注意,这仅在您的函数返回的结果为 False 的情况下才有效,例如 None 或 0.如果它返回的结果为 True 在某些时候,例如 1,它不会应用于迭代器中的任何剩余元素.为了安全起见,这个习惯用法主要用于没有返回语句的函数.

                  Note that this only works as long as your function returns something that evaluates to False, e.g., None or 0. If it does return something that evaluates to True at some point, e.g., 1, it will not be applied to any of the remaining elements in your iterator. To be safe, use this idiom mainly for functions without return statement.

                  这篇关于python中是否有没有结果的地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:范围(len(list))还是枚举(list)? 下一篇:列表或元组的成对遍历

                  相关文章

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

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

                      • <bdo id='FX8Z3'></bdo><ul id='FX8Z3'></ul>
                    1. <tfoot id='FX8Z3'></tfoot>