文章来自《Python cookbook》.

翻译仅仅是为了个人学习,其它商业版权纠纷与此无关!

-- Zoom.Quiet [2004-08-11 01:03:22]

1. 使用list包容代替map和filter

1.11 Using List Comprehensions Instead of map and filter

使用list包容代替map和filter

Credit: Luther Blissett

1.1. 问题 Problem

1.11.1 Problem

You want to perform an operation on all the elements of a list, but you'd like to avoid using map and filter because they can be hard to read and understand, particularly when they need lambda.

你想在一个list上的所有元素上执行一个操作.但是你想避免使用map和filter .因为他们难以阅读和理解, 特别是当使用lambda的时候.

1.2. 解决 Solution

1.11.2 Solution

Say you want to create a new list by adding 23 to each item of some other list. In Python 1.5.2, the solution is:

假定你想通过给一些其他list的每个新的元素增加23来建立一个新的list . 在python1.5.2 , 方案是:

   1 thenewlist = map(lambda x: x + 23, theoldlist)

This is hardly the clearest code. Fortunately, since Python 2.0, we can use a list comprehension instead:

这几乎不是最清晰的代码.幸运的是,自python2.0以来.我们能使用list包容来代替:

   1 thenewlist = [x + 23 for x in theoldlist]

This is much clearer and more elegant.

这个是更清楚和更优雅的

Similarly, say you want the new list to comprise all items in the other list that are larger than 5. In Python 1.5.2, the solution is:

类似地,假定你想把一个别的list所有大于5的元素来作为一个新的list.在python1.5.2里,方案是:

   1 thenewlist = filter(lambda x: x > 5, theoldlist)

But in modern Python, we can use the following list comprehension:

但是在新的python里,我们能使用下面的的list包容:

   1 thenewlist = [x for x in theoldlist if x > 5]

Now say you want to combine both list operations. In Python 1.5.2, the solution is quite complex:

现在假定你想合并这两个list的操作.在python1.5.2里,方案是相当的复杂:

   1 thenewlist = map(lambda x: x+23, filter(lambda x: x>5, theoldlist))

A list comprehension affords far greater clarity, as we can both perform selection with the if clause and use some expression, such as adding 23, on the selected items:

一个list包容提供更加清晰的方案.因为我们能既能在所选择的元素上执行if子句并且可以使用一些表达式,诸如,在选择的元素上加23

   1 thenewlist = [x + 23 for x in theoldlist if x > 5]

1.3. 讨论 Discussion

1.11.3 Discussion

Elegance and clarity, within a generally pragmatic attitude, are Python's core values. List comprehensions, added in Python 2.0, delightfully display how pragmatism can enhance both clarity and elegance. The built-in map and filter functions still have their uses, since they're arguably of equal elegance and clarity as list comprehensions when the lambda construct is not necessary. In fact, when their first argument is another built-in function (i.e., when lambda is not involved and there is no need to write a function just for the purpose of using it within a map or filter), they can be even faster than list comprehensions.

在一般实用的看法中,优雅和清晰是python的核心价值.list包容,在python2.0中增加,令人可喜地显示出实用主义能够提高清晰和优雅.内建的map和filter函数仍然有他们的用处.因为象证明的那样,当lambda不是必要的时候他们和list包容一样的清晰和优雅.实际上,当他们的第一个参数是其他内建的函数时(当不相关lambda,并且不需要写一个函数.仅仅为了使用map和filter的目的)他们甚至比list包容要快.

All in all, Python programs optimally written for 2.0 or later use far fewer map and filter calls than similar programs written for 1.5.2. Most of the map and filter calls (and quite a few explicit loops) are replaced with list comprehensions (which Python borrowed, after some prettying of the syntax, from Haskell, described at http://www.haskell.org). It's not an issue of wanting to play with a shiny new toy (although that desire, too, has its place in a programmer's heart)梩he point is that the toy, when used well, is a wonderfully useful instrument, further enhancing your Python programs' clarity, simplicity, and elegance.

总而言之,python程序最理想的是,在2.0 或者以后版本中使用比在1.5.2里类似的程序更少的map和filter调用.map和filter的大部分都可以使用list包容来替换

(它是python从 Haskell借来的一些漂亮语法.描述在http://www.haskell.org).

  • 这不是一个想玩漂亮新玩具的问题(尽管这个愿望在程序员心中有一定地位).我们的观点是,这个玩具,使用的好的时候,它是一件强有力的工具,它会让你的python程序更清晰,简单和优雅.

1.4. 参考 See Also

The Reference Manual section on list displays (the other name for list comprehensions).