From 9c783795d268b46a70150ca86a1ec675599b510b Mon Sep 17 00:00:00 2001 From: Louie Lu Date: Wed, 5 Apr 2017 13:07:02 +0800 Subject: [PATCH 1/7] bpo-29981: Update Index for set, dict, and generator 'comprehensions' --- Doc/glossary.rst | 11 +++++++++++ Doc/reference/expressions.rst | 22 +++++++++++++++------- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/Doc/glossary.rst b/Doc/glossary.rst index dba9186d935a6a..b10d561c55ddf0 100644 --- a/Doc/glossary.rst +++ b/Doc/glossary.rst @@ -272,6 +272,12 @@ Glossary keys can be any object with :meth:`__hash__` and :meth:`__eq__` methods. Called a hash in Perl. + dictionary comprehension + A compact way to process all or part of the pair elements in a sequence + and return a dictionary with the results. ``results = {n: n ** 2 for n in + range(10)}`` generates a dictionary containing key ``n`` which mapped to + value ``n ** 2``. + dictionary view The objects returned from :meth:`dict.keys`, :meth:`dict.values`, and :meth:`dict.items` are called dictionary views. They provide a dynamic @@ -921,6 +927,11 @@ Glossary reserved for rare cases where there are large numbers of instances in a memory-critical application. + set comprehension + A compact way to process all or part of the elements in a sequence and + return a set with the results. ``results = {'foo' for _ in range(10)}`` + generates a set of strings with ``foo``. + sequence An :term:`iterable` which supports efficient element access using integer indices via the :meth:`__getitem__` special method and defines a diff --git a/Doc/reference/expressions.rst b/Doc/reference/expressions.rst index c4f6c55c7ca3aa..16bbc1abeee24a 100644 --- a/Doc/reference/expressions.rst +++ b/Doc/reference/expressions.rst @@ -160,6 +160,8 @@ ambiguities and allow common typos to pass uncaught. Displays for lists, sets and dictionaries ----------------------------------------- +.. index:: single: comprehensions + For constructing a list, a set or a dictionary Python provides special syntax called "displays", each of them in two flavors: @@ -227,8 +229,10 @@ the list is constructed from the elements resulting from the comprehension. Set displays ------------ -.. index:: pair: set; display - object: set +.. index:: + pair: set; display + pair: set; comprehensions + object: set A set display is denoted by curly braces and distinguishable from dictionary displays by the lack of colons separating keys and values: @@ -251,9 +255,11 @@ dictionary. Dictionary displays ------------------- -.. index:: pair: dictionary; display - key, datum, key/datum pair - object: dictionary +.. index:: + pair: dictionary; display + pair: dictionary; comprehensions + key, datum, key/datum pair + object: dictionary A dictionary display is a possibly empty series of key/datum pairs enclosed in curly braces: @@ -302,8 +308,10 @@ prevails. Generator expressions --------------------- -.. index:: pair: generator; expression - object: generator +.. index:: + pair: generator; expression + pair: generator; comprehensions + object: generator A generator expression is a compact generator notation in parentheses: From f076f008f093125f33729f2a58bdd8db20562e3c Mon Sep 17 00:00:00 2001 From: Louie Lu Date: Mon, 10 Apr 2017 12:18:37 +0800 Subject: [PATCH 2/7] Addressed shrhiy-storchaka problem --- Doc/glossary.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/glossary.rst b/Doc/glossary.rst index b10d561c55ddf0..bd1a9bf23ae823 100644 --- a/Doc/glossary.rst +++ b/Doc/glossary.rst @@ -929,8 +929,8 @@ Glossary set comprehension A compact way to process all or part of the elements in a sequence and - return a set with the results. ``results = {'foo' for _ in range(10)}`` - generates a set of strings with ``foo``. + return a set with the results. ``results = {c for c in 'abracadabra' if + c not in 'abc'`` generates a set of strings with ``{'r', 'd'}``. sequence An :term:`iterable` which supports efficient element access using integer From 1337ee887572c1f54b9356414447a6c2cb31d567 Mon Sep 17 00:00:00 2001 From: Louie Lu Date: Mon, 10 Apr 2017 14:23:57 +0800 Subject: [PATCH 3/7] Fix typo --- Doc/glossary.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/glossary.rst b/Doc/glossary.rst index bd1a9bf23ae823..7675fd566b8328 100644 --- a/Doc/glossary.rst +++ b/Doc/glossary.rst @@ -930,7 +930,7 @@ Glossary set comprehension A compact way to process all or part of the elements in a sequence and return a set with the results. ``results = {c for c in 'abracadabra' if - c not in 'abc'`` generates a set of strings with ``{'r', 'd'}``. + c not in 'abc'}`` generates a set of strings with ``{'r', 'd'}``. sequence An :term:`iterable` which supports efficient element access using integer From d9e7c167f14d6094db02f240adf055a08b7e47b0 Mon Sep 17 00:00:00 2001 From: Louie Lu Date: Mon, 10 Apr 2017 14:24:19 +0800 Subject: [PATCH 4/7] Addressed from serhiy-storchaka --- Doc/library/stdtypes.rst | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index a9877ba666d3f7..3168f39f311424 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -3826,9 +3826,11 @@ another set. The :class:`frozenset` type is immutable and :term:`hashable` --- its contents cannot be altered after it is created; it can therefore be used as a dictionary key or as an element of another set. -Non-empty sets (not frozensets) can be created by placing a comma-separated list -of elements within braces, for example: ``{'jack', 'sjoerd'}``, in addition to the -:class:`set` constructor. +Non-empty sets (not frozensets) canb be created by several ways: + +* Using a comma-separated list of elements within braces: ``{'jack', 'sjoerd'}`` +* Using :class:`set` constructor: ``set('foobar')``, ``set(['a', 'b', 'foo'])`` +* Using set comprehension: ``{c for c in 'abracadabra' if c not in 'abc'}`` The constructors for both classes work the same: @@ -4024,9 +4026,13 @@ then they can be used interchangeably to index the same dictionary entry. (Note however, that since computers store floating-point numbers as approximations it is usually unwise to use them as dictionary keys.) -Dictionaries can be created by placing a comma-separated list of ``key: value`` -pairs within braces, for example: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098: -'jack', 4127: 'sjoerd'}``, or by the :class:`dict` constructor. +Dictionaries can be created in several ways: + +* Using a comma-separated list of ``key: value`` pairs within braces: + ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098: 'jack', 4127: 'sjoerd'}`` +* Using :class:`dict` constructor: ``dict()``, + ``dict([('foo', 100), ('bar', 200)])``, ``dict(foo=100, bar=200)`` +* Using dict comprehension: ``{x: x ** 2 for x in range(10)}`` .. class:: dict(**kwarg) dict(mapping, **kwarg) From 5e4cbfa735a76c4065296564cfdb0a3965cbe8c3 Mon Sep 17 00:00:00 2001 From: Louie Lu Date: Mon, 10 Apr 2017 15:23:32 +0800 Subject: [PATCH 5/7] Fix dict and set comprehension --- Doc/library/stdtypes.rst | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 3168f39f311424..0c6269134d9847 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -3826,11 +3826,13 @@ another set. The :class:`frozenset` type is immutable and :term:`hashable` --- its contents cannot be altered after it is created; it can therefore be used as a dictionary key or as an element of another set. -Non-empty sets (not frozensets) canb be created by several ways: +Sets can be created by several ways: * Using a comma-separated list of elements within braces: ``{'jack', 'sjoerd'}`` -* Using :class:`set` constructor: ``set('foobar')``, ``set(['a', 'b', 'foo'])`` -* Using set comprehension: ``{c for c in 'abracadabra' if c not in 'abc'}`` +* Using :class:`set` constructor: ``set()``, ``set('foobar')``, + ``set(['a', 'b', 'foo'])`` +* Using a set comprehension: ``{x for x in ()}``, + ``{c for c in 'abracadabra' if c not in 'abc'}`` The constructors for both classes work the same: @@ -4032,7 +4034,7 @@ Dictionaries can be created in several ways: ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098: 'jack', 4127: 'sjoerd'}`` * Using :class:`dict` constructor: ``dict()``, ``dict([('foo', 100), ('bar', 200)])``, ``dict(foo=100, bar=200)`` -* Using dict comprehension: ``{x: x ** 2 for x in range(10)}`` +* Using a dict comprehension: ``{}``, ``{x: x ** 2 for x in range(10)}`` .. class:: dict(**kwarg) dict(mapping, **kwarg) From e9093654e55053e4873e4fe30f84b1e56d03aa42 Mon Sep 17 00:00:00 2001 From: Louie Lu Date: Wed, 26 Apr 2017 14:41:39 +0800 Subject: [PATCH 6/7] Addressed bitdancer problem --- Doc/glossary.rst | 7 ++++--- Doc/library/stdtypes.rst | 30 ++++++++++++++---------------- 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/Doc/glossary.rst b/Doc/glossary.rst index 7675fd566b8328..8e9d7730382cb1 100644 --- a/Doc/glossary.rst +++ b/Doc/glossary.rst @@ -273,10 +273,10 @@ Glossary Called a hash in Perl. dictionary comprehension - A compact way to process all or part of the pair elements in a sequence + A compact way to process all or part of the items in a sequence and return a dictionary with the results. ``results = {n: n ** 2 for n in range(10)}`` generates a dictionary containing key ``n`` which mapped to - value ``n ** 2``. + value ``n ** 2``. See :ref:`comprehensions`. dictionary view The objects returned from :meth:`dict.keys`, :meth:`dict.values`, and @@ -930,7 +930,8 @@ Glossary set comprehension A compact way to process all or part of the elements in a sequence and return a set with the results. ``results = {c for c in 'abracadabra' if - c not in 'abc'}`` generates a set of strings with ``{'r', 'd'}``. + c not in 'abc'}`` generates the set of strings with ``{'r', 'd'}``. See + :ref:`comprehensions`. sequence An :term:`iterable` which supports efficient element access using integer diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 0c6269134d9847..30683255774cc6 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -3826,19 +3826,17 @@ another set. The :class:`frozenset` type is immutable and :term:`hashable` --- its contents cannot be altered after it is created; it can therefore be used as a dictionary key or as an element of another set. -Sets can be created by several ways: - -* Using a comma-separated list of elements within braces: ``{'jack', 'sjoerd'}`` -* Using :class:`set` constructor: ``set()``, ``set('foobar')``, - ``set(['a', 'b', 'foo'])`` -* Using a set comprehension: ``{x for x in ()}``, - ``{c for c in 'abracadabra' if c not in 'abc'}`` - The constructors for both classes work the same: .. class:: set([iterable]) frozenset([iterable]) + Sets can be created by several ways:b + + * Using a comma-separated list of elements within braces: ``{'jack', 'sjoerd'}`` + * Using a set comprehension: ``{c for c in 'abracadabra' if c not in 'abc'}`` + * Using the type constructor: ``set()``, ``set('foobar')``, ``set(['a', 'b', 'foo'])`` + Return a new set or frozenset object whose elements are taken from *iterable*. The elements of a set must be :term:`hashable`. To represent sets of sets, the inner sets must be :class:`frozenset` @@ -4028,18 +4026,18 @@ then they can be used interchangeably to index the same dictionary entry. (Note however, that since computers store floating-point numbers as approximations it is usually unwise to use them as dictionary keys.) -Dictionaries can be created in several ways: - -* Using a comma-separated list of ``key: value`` pairs within braces: - ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098: 'jack', 4127: 'sjoerd'}`` -* Using :class:`dict` constructor: ``dict()``, - ``dict([('foo', 100), ('bar', 200)])``, ``dict(foo=100, bar=200)`` -* Using a dict comprehension: ``{}``, ``{x: x ** 2 for x in range(10)}`` - .. class:: dict(**kwarg) dict(mapping, **kwarg) dict(iterable, **kwarg) + Dictionaries can be created in several ways: + + * Using a comma-separated list of ``key: value`` pairs within braces: + ``{'jack': 4098, 'sjoerd': 4127}`` or ``{4098: 'jack', 4127: 'sjoerd'}`` + * Using a dict comprehension: ``{}``, ``{x: x ** 2 for x in range(10)}`` + * Using the type constructor: ``dict()``, + ``dict([('foo', 100), ('bar', 200)])``, ``dict(foo=100, bar=200)`` + Return a new dictionary initialized from an optional positional argument and a possibly empty set of keyword arguments. From 073256a5f2398cc522169078d77c5bcc1245188a Mon Sep 17 00:00:00 2001 From: Louie Lu Date: Wed, 26 Apr 2017 22:33:55 +0800 Subject: [PATCH 7/7] Remove trailing character --- Doc/library/stdtypes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 30683255774cc6..e5abe8b1cda8f4 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -3831,7 +3831,7 @@ The constructors for both classes work the same: .. class:: set([iterable]) frozenset([iterable]) - Sets can be created by several ways:b + Sets can be created by several ways: * Using a comma-separated list of elements within braces: ``{'jack', 'sjoerd'}`` * Using a set comprehension: ``{c for c in 'abracadabra' if c not in 'abc'}``