[Python-checkins] python/dist/src/Doc/tut tut.tex,1.164,1.165
rhettinger@users.sourceforge.net
rhettinger@users.sourceforge.net
Tue, 25 Jun 2002 08:13:20 -0700
Update of /cvsroot/python/python/dist/src/Doc/tut
In directory usw-pr-cvs1:/tmp/cvs-serv32056
Modified Files:
tut.tex
Log Message:
Close bug 480337: Dict used before dicts explained. Added explanation
and examples of the dict() constructor.
Index: tut.tex
===================================================================
RCS file: /cvsroot/python/python/dist/src/Doc/tut/tut.tex,v
retrieving revision 1.164
retrieving revision 1.165
diff -C2 -d -r1.164 -r1.165
*** tut.tex 25 Jun 2002 03:17:03 -0000 1.164
--- tut.tex 25 Jun 2002 15:13:18 -0000 1.165
***************
*** 1844,1849 ****
>>> [3*x for x in vec if x < 2]
[]
- >>> [{x: x**2} for x in vec]
- [{2: 4}, {4: 16}, {6: 36}]
>>> [[x,x**2] for x in vec]
[[2, 4], [4, 16], [6, 36]]
--- 1844,1847 ----
***************
*** 2022,2025 ****
--- 2020,2034 ----
>>> tel.has_key('guido')
1
+ \end{verbatim}
+
+ The \function{dict()} contructor 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.
+
+ \begin{verbatim}
+ >>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
+ {'sape': 4139, 'jack': 4098, 'guido': 4127}
+ >>> dict([(x, x**2) for x in vec]) # use a list comprehension
+ {2: 4, 4: 16, 6: 36}
\end{verbatim}