<bdo id='8Q95h'></bdo><ul id='8Q95h'></ul>

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

        <legend id='8Q95h'><style id='8Q95h'><dir id='8Q95h'><q id='8Q95h'></q></dir></style></legend><tfoot id='8Q95h'></tfoot>

        如何在 Python 3 中使用过滤器、映射和归约

        How to use filter, map, and reduce in Python 3(如何在 Python 3 中使用过滤器、映射和归约)

          <tbody id='memae'></tbody>
        • <legend id='memae'><style id='memae'><dir id='memae'><q id='memae'></q></dir></style></legend>

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

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

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

                  本文介绍了如何在 Python 3 中使用过滤器、映射和归约的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  filtermapreduce 在 Python 2 中完美运行.下面是一个示例:

                  filter, map, and reduce work perfectly in Python 2. Here is an example:

                  >>> def f(x):
                          return x % 2 != 0 and x % 3 != 0
                  >>> filter(f, range(2, 25))
                  [5, 7, 11, 13, 17, 19, 23]
                  
                  >>> def cube(x):
                          return x*x*x
                  >>> map(cube, range(1, 11))
                  [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
                  
                  >>> def add(x,y):
                          return x+y
                  >>> reduce(add, range(1, 11))
                  55
                  

                  但在 Python 3 中,我收到以下输出:

                  But in Python 3, I receive the following outputs:

                  >>> filter(f, range(2, 25))
                  <filter object at 0x0000000002C14908>
                  
                  >>> map(cube, range(1, 11))
                  <map object at 0x0000000002C82B70>
                  
                  >>> reduce(add, range(1, 11))
                  Traceback (most recent call last):
                    File "<pyshell#8>", line 1, in <module>
                      reduce(add, range(1, 11))
                  NameError: name 'reduce' is not defined
                  

                  如果有人能向我解释为什么会这样,我将不胜感激.

                  I would appreciate if someone could explain to me why this is.

                  代码截图更清楚:

                  推荐答案

                  你可以阅读 Python 3.0 的新特性.当您从 2.x 迁移到 3.x 时,您应该仔细阅读它,因为已经发生了很多变化.

                  You can read about the changes in What's New In Python 3.0. You should read it thoroughly when you move from 2.x to 3.x since a lot has been changed.

                  这里的全部答案是从文档中引用的.

                  The whole answer here are quotes from the documentation.

                  视图和迭代器而不是列表

                  一些知名 API 不再返回列表:

                  Some well-known APIs no longer return lists:

                  • [...]
                  • map()filter() 返回迭代器.如果你真的需要一个列表,一个快速修复是例如list(map(...)),但更好的解决方法通常是使用列表推导式(尤其是当原始代码使用 lambda 时),或者重写代码使其不需要列表一点也不.特别棘手的是为函数的副作用调用 map();正确的转换是使用常规的 for 循环(因为创建列表只是浪费).
                  • [...]
                  • [...]
                  • map() and filter() return iterators. If you really need a list, a quick fix is e.g. list(map(...)), but a better fix is often to use a list comprehension (especially when the original code uses lambda), or rewriting the code so it doesn’t need a list at all. Particularly tricky is map() invoked for the side effects of the function; the correct transformation is to use a regular for loop (since creating a list would just be wasteful).
                  • [...]

                  内建

                  • [...]
                  • 删除了 reduce().使用 functools.reduce()如果你真的需要它;但是,在 99% 的情况下,显式 for 循环更具可读性.
                  • [...]
                  • [...]
                  • Removed reduce(). Use functools.reduce() if you really need it; however, 99 percent of the time an explicit for loop is more readable.
                  • [...]

                  这篇关于如何在 Python 3 中使用过滤器、映射和归约的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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;?(为什么我的函数输出打印出“无?)

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

                            <tbody id='JUk3F'></tbody>

                          <tfoot id='JUk3F'></tfoot>

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