Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
7dccde38
Commit
7dccde38
authored
Nov 17, 2012
by
Ezio Melotti
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#16470: mention set and dict comprehension in the tutorial. Patch by Yongzhi Pan.
parent
8ab5aaf3
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
19 additions
and
8 deletions
+19
-8
Doc/tutorial/datastructures.rst
Doc/tutorial/datastructures.rst
+19
-8
No files found.
Doc/tutorial/datastructures.rst
View file @
7dccde38
...
@@ -229,6 +229,7 @@ Don't use this example's definition of :func:`sum`: since summing numbers is
...
@@ -229,6 +229,7 @@ Don't use this example's definition of :func:`sum`: since summing numbers is
such a common need, a built-in function ``sum(sequence)`` is already provided,
such a common need, a built-in function ``sum(sequence)`` is already provided,
and works exactly like this.
and works exactly like this.
.. _tut-listcomps:
List Comprehensions
List Comprehensions
-------------------
-------------------
...
@@ -485,6 +486,10 @@ with no duplicate elements. Basic uses include membership testing and
...
@@ -485,6 +486,10 @@ with no duplicate elements. Basic uses include membership testing and
eliminating duplicate entries. Set objects also support mathematical operations
eliminating duplicate entries. Set objects also support mathematical operations
like union, intersection, difference, and symmetric difference.
like union, intersection, difference, and symmetric difference.
Curly braces or the :func:`set` function can be used to create sets. Note: to
create an empty set you have to use ``set()``, not ``{}``; the latter creates an
empty dictionary, a data structure that we discuss in the next section.
Here is a brief demonstration::
Here is a brief demonstration::
>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
...
@@ -511,6 +516,13 @@ Here is a brief demonstration::
...
@@ -511,6 +516,13 @@ Here is a brief demonstration::
>>> a ^ b # letters in a or b but not both
>>> a ^ b # letters in a or b but not both
set(['r', 'd', 'b', 'm', 'z', 'l'])
set(['r', 'd', 'b', 'm', 'z', 'l'])
Similarly to :ref:`list comprehensions <tut-listcomps>`, set comprehensions
are also supported::
>>> a = {x for x in 'abracadabra' if x not in 'abc'}
>>> a
set(['r', 'd'])
.. _tut-dictionaries:
.. _tut-dictionaries:
...
@@ -562,18 +574,17 @@ Here is a small example using a dictionary::
...
@@ -562,18 +574,17 @@ Here is a small example using a dictionary::
>>> 'guido' in tel
>>> 'guido' in tel
True
True
The :func:`dict` constructor builds dictionaries directly from lists of
The :func:`dict` constructor builds dictionaries directly from sequences of
key-value pairs stored as tuples. When the pairs form a pattern, list
key-value pairs::
comprehensions can compactly specify the key-value list. ::
>>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
>>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
{'sape': 4139, 'jack': 4098, 'guido': 4127}
{'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
In addition, dict comprehensions can be used to create dictionaries from
better suited for the task of supplying key-values pairs to the :func:`dict`
arbitrary key and value expressions::
constructor.
>>> {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
When the keys are simple strings, it is sometimes easier to specify pairs using
keyword arguments::
keyword arguments::
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment