summaryrefslogtreecommitdiffstats
path: root/Doc/tutorial/datastructures.rst
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2008-02-01 11:56:49 (GMT)
committerGeorg Brandl <georg@python.org>2008-02-01 11:56:49 (GMT)
commitf69451833191454bfef75804c2654dc37e8f3e93 (patch)
tree7e81560f5276c35f68b7b02e75feb9221a82ae5d /Doc/tutorial/datastructures.rst
parentf25ef50549d9f2bcb6294fe61a9902490728edcc (diff)
downloadcpython-f69451833191454bfef75804c2654dc37e8f3e93.zip
cpython-f69451833191454bfef75804c2654dc37e8f3e93.tar.gz
cpython-f69451833191454bfef75804c2654dc37e8f3e93.tar.bz2
Update docs w.r.t. PEP 3100 changes -- patch for GHOP by Dan Finnie.
Diffstat (limited to 'Doc/tutorial/datastructures.rst')
-rw-r--r--Doc/tutorial/datastructures.rst23
1 files changed, 15 insertions, 8 deletions
diff --git a/Doc/tutorial/datastructures.rst b/Doc/tutorial/datastructures.rst
index 206f056..e2f218c 100644
--- a/Doc/tutorial/datastructures.rst
+++ b/Doc/tutorial/datastructures.rst
@@ -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
==================