mirror of https://github.com/python/cpython.git
Expand a bit on dict views.
This commit is contained in:
parent
5e06a65639
commit
fc11f27f61
|
@ -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*.
|
||||
|
|
Loading…
Reference in New Issue