parent
ebc3175f87
commit
ff898c8e8a
33
pep-0202.txt
33
pep-0202.txt
|
@ -5,14 +5,14 @@ Owner: tpeters@beopen.com (Tim Peters)
|
|||
Python-Version: 2.0
|
||||
Status: Incomplete
|
||||
|
||||
|
||||
|
||||
Introduction
|
||||
|
||||
This PEP describes a proposed syntactical extension to Python, list
|
||||
comprehensions.
|
||||
|
||||
|
||||
|
||||
|
||||
The Proposed Solution
|
||||
|
||||
It is proposed to allow conditional construction of list literals using
|
||||
|
@ -20,14 +20,14 @@ The Proposed Solution
|
|||
statements nest now.
|
||||
|
||||
|
||||
|
||||
|
||||
Rationale
|
||||
|
||||
List comprehensions provide a more concise way to create lists in
|
||||
situations where map() and filter() and/or nested loops would currently
|
||||
be used.
|
||||
|
||||
|
||||
|
||||
Examples
|
||||
|
||||
>>> print [i for i in range(10)]
|
||||
|
@ -38,17 +38,17 @@ Examples
|
|||
|
||||
>>> nums = [1,2,3,4]
|
||||
>>> fruit = ["Apples", "Peaches", "Pears", "Bananas"]
|
||||
>>> print [i,f for i in nums for f in fruit]
|
||||
>>> print [(i,f) for i in nums for f in fruit]
|
||||
[(1, 'Apples'), (1, 'Peaches'), (1, 'Pears'), (1, 'Bananas'),
|
||||
(2, 'Apples'), (2, 'Peaches'), (2, 'Pears'), (2, 'Bananas'),
|
||||
(3, 'Apples'), (3, 'Peaches'), (3, 'Pears'), (3, 'Bananas'),
|
||||
(4, 'Apples'), (4, 'Peaches'), (4, 'Pears'), (4, 'Bananas')]
|
||||
>>> print [i,f for i in nums for f in fruit if f[0] == "P"]
|
||||
>>> print [(i,f) for i in nums for f in fruit if f[0] == "P"]
|
||||
[(1, 'Peaches'), (1, 'Pears'),
|
||||
(2, 'Peaches'), (2, 'Pears'),
|
||||
(3, 'Peaches'), (3, 'Pears'),
|
||||
(4, 'Peaches'), (4, 'Pears')]
|
||||
>>> print [i,f for i in nums for f in fruit if f[0] == "P" if i%2 == 1]
|
||||
>>> print [(i,f) for i in nums for f in fruit if f[0] == "P" if i%2 == 1]
|
||||
[(1, 'Peaches'), (1, 'Pears'), (3, 'Peaches'), (3, 'Pears')]
|
||||
>>> def zip(*args):
|
||||
... return apply(map, (None,)+args)
|
||||
|
@ -56,7 +56,7 @@ Examples
|
|||
>>> print [i for i in zip(nums,fruit) if i[0]%2==0]
|
||||
[(2, 'Peaches'), (4, 'Bananas')]
|
||||
|
||||
|
||||
|
||||
Reference Implementation
|
||||
|
||||
Please refer to
|
||||
|
@ -66,7 +66,20 @@ Reference Implementation
|
|||
for a patch that adds list comprehensions to Python.
|
||||
|
||||
|
||||
|
||||
BDFL Pronouncements
|
||||
|
||||
Note: the BDFL refers to Guido van Rossum, Python's Benevolent
|
||||
Dictator For Life.
|
||||
|
||||
- The syntax proposed above is the Right One.
|
||||
|
||||
- The form [x, y for ...] should be disallowed; one should be
|
||||
required to write [(x, y) for ...].
|
||||
|
||||
- The form [... for x... for y...] nests, with the last index
|
||||
varying fastest, just like nested for loops.
|
||||
|
||||
|
||||
Open Issues
|
||||
|
||||
Syntax
|
||||
|
@ -113,7 +126,7 @@ Open Issues
|
|||
is. It has not had a lot of exercise, though the patch does include
|
||||
a few test cases.
|
||||
|
||||
|
||||
|
||||
Local Variables:
|
||||
mode: indented-text
|
||||
indent-tabs-mode: nil
|
||||
|
|
Loading…
Reference in New Issue