mirror of
https://github.com/python/cpython.git
synced 2025-11-02 03:01:58 +00:00
Update docs w.r.t. PEP 3100 changes -- patch for GHOP by Dan Finnie.
This commit is contained in:
parent
f25ef50549
commit
f694518331
48 changed files with 395 additions and 534 deletions
|
|
@ -273,7 +273,7 @@ Here are some nested for loops and other fancy behavior::
|
|||
|
||||
List comprehensions can be applied to complex expressions and nested functions::
|
||||
|
||||
>>> [str(round(355/113.0, i)) for i in range(1, 6)]
|
||||
>>> [str(round(355/113, i)) for i in range(1, 6)]
|
||||
['3.1', '3.14', '3.142', '3.1416', '3.14159']
|
||||
|
||||
|
||||
|
|
@ -437,6 +437,9 @@ Here is a brief demonstration::
|
|||
>>> fruit = set(basket) # create a set without duplicates
|
||||
>>> fruit
|
||||
{'orange', 'pear', 'apple', 'banana'}
|
||||
>>> fruit = {'orange', 'apple'} # {} syntax is equivalent to [] for lists
|
||||
>>> fruit
|
||||
{'orange', 'apple'}
|
||||
>>> 'orange' in fruit # fast membership testing
|
||||
True
|
||||
>>> 'crabgrass' in fruit
|
||||
|
|
@ -457,6 +460,11 @@ Here is a brief demonstration::
|
|||
>>> a ^ b # letters in a or b but not both
|
||||
{'r', 'd', 'b', 'm', 'z', 'l'}
|
||||
|
||||
Like for lists, there is a set comprehension syntax::
|
||||
|
||||
>>> a = {x for x in 'abracadabra' if x not in 'abc'}
|
||||
>>> a
|
||||
{'r', 'd'}
|
||||
|
||||
|
||||
|
||||
|
|
@ -518,12 +526,12 @@ comprehensions can compactly specify the key-value list. ::
|
|||
|
||||
>>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
|
||||
{'sape': 4139, 'jack': 4098, 'guido': 4127}
|
||||
>>> dict([(x, x**2) for x in (2, 4, 6)]) # use a list comprehension
|
||||
{2: 4, 4: 16, 6: 36}
|
||||
|
||||
Later in the tutorial, we will learn about Generator Expressions which are even
|
||||
better suited for the task of supplying key-values pairs to the :func:`dict`
|
||||
constructor.
|
||||
In addition, dict comprehensions can be used to create dictionaries from
|
||||
arbitrary key and value expressions::
|
||||
|
||||
>>> {x: x**2 for x in (2, 4, 6)}
|
||||
{2: 4, 4: 16, 6: 36}
|
||||
|
||||
When the keys are simple strings, it is sometimes easier to specify pairs using
|
||||
keyword arguments::
|
||||
|
|
@ -532,9 +540,8 @@ keyword arguments::
|
|||
{'sape': 4139, 'jack': 4098, 'guido': 4127}
|
||||
|
||||
|
||||
.. XXX Find out the right way to do these DUBOIS
|
||||
.. _tut-loopidioms:
|
||||
.. %
|
||||
Find out the right way to do these DUBOIS
|
||||
|
||||
Looping Techniques
|
||||
==================
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue