diff options
author | Georg Brandl <georg@python.org> | 2009-06-16 19:22:10 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2009-06-16 19:22:10 (GMT) |
commit | fc11f27f616fe875d3ce01a51cb2498dc7c86500 (patch) | |
tree | 2bae18fe1cde6afd150c3a51503ec5b44f2ec29e /Doc/tutorial/datastructures.rst | |
parent | 5e06a65639e26c3cf0716c4acc9086cc54b3eef4 (diff) | |
download | cpython-fc11f27f616fe875d3ce01a51cb2498dc7c86500.zip cpython-fc11f27f616fe875d3ce01a51cb2498dc7c86500.tar.gz cpython-fc11f27f616fe875d3ce01a51cb2498dc7c86500.tar.bz2 |
Expand a bit on dict views.
Diffstat (limited to 'Doc/tutorial/datastructures.rst')
-rw-r--r-- | Doc/tutorial/datastructures.rst | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/Doc/tutorial/datastructures.rst b/Doc/tutorial/datastructures.rst index 7b718e8..58dd614 100644 --- a/Doc/tutorial/datastructures.rst +++ b/Doc/tutorial/datastructures.rst @@ -154,6 +154,8 @@ the queue, use :meth:`pop` with ``0`` as the index. For example:: ['Michael', 'Terry', 'Graham'] +.. _tut-listcomps: + List Comprehensions ------------------- @@ -401,7 +403,7 @@ 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:: +Like :ref:`for lists <tut-listcomps>`, there is a set comprehension syntax:: >>> a = {x for x in 'abracadabra' if x not in 'abc'} >>> a @@ -438,9 +440,9 @@ value associated with that key is forgotten. It is an error to extract a value using a non-existent key. Performing ``list(d.keys())`` on a dictionary returns a list of all the keys -used in the dictionary, in arbitrary order (if you want it sorted, just apply -the :meth:`sorted` function instead). To check whether a single key is -in the dictionary, use the :keyword:`in` keyword. +used in the dictionary, in arbitrary order (if you want it sorted, just use +``sorted(d.keys())`` instead). [1]_ To check whether a single key is in the +dictionary, use the :keyword:`in` keyword. Here is a small example using a dictionary:: @@ -463,9 +465,8 @@ Here is a small example using a dictionary:: >>> 'jack' not in tel False -The :func:`dict` constructor builds dictionaries directly from lists of -key-value pairs stored as tuples. When the pairs form a pattern, list -comprehensions can compactly specify the key-value list. :: +The :func:`dict` constructor builds dictionaries directly from sequences of +key-value pairs stored as tuples. :: >>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)]) {'sape': 4139, 'jack': 4098, 'guido': 4127} @@ -483,7 +484,6 @@ keyword arguments:: {'sape': 4139, 'jack': 4098, 'guido': 4127} -.. XXX Find out the right way to do these DUBOIS .. _tut-loopidioms: Looping Techniques @@ -604,9 +604,9 @@ sequence is exhausted. If two items to be compared are themselves sequences of the same type, the lexicographical comparison is carried out recursively. If all items of two sequences compare equal, the sequences are considered equal. If one sequence is an initial sub-sequence of the other, the shorter sequence is -the smaller (lesser) one. Lexicographical ordering for strings uses the ASCII -ordering for individual characters. Some examples of comparisons between -sequences of the same type:: +the smaller (lesser) one. Lexicographical ordering for strings uses the Unicode +codepoint number to order individual characters. Some examples of comparisons +between sequences of the same type:: (1, 2, 3) < (1, 2, 4) [1, 2, 3] < [1, 2, 4] @@ -621,3 +621,10 @@ provided that the objects have appropriate comparison methods. For example, mixed numeric types are compared according to their numeric value, so 0 equals 0.0, etc. Otherwise, rather than providing an arbitrary ordering, the interpreter will raise a :exc:`TypeError` exception. + + +.. rubric:: Footnotes + +.. [1] Calling ``d.keys()`` will return a :dfn:`dictionary view` object. It + supports operations like membership test and iteration, but its contents + are not independent of the original dictionary -- it is only a *view*. |