mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00

svn+ssh://pythondev@svn.python.org/python/trunk ........ r53624 | peter.astrand | 2007-02-02 20:06:36 +0100 (Fri, 02 Feb 2007) | 1 line We had several if statements checking the value of a fd. This is unsafe, since valid fds might be zero. We should check for not None instead. ........ r53635 | kurt.kaiser | 2007-02-05 07:03:18 +0100 (Mon, 05 Feb 2007) | 2 lines Add 'raw' support to configHandler. Patch 1650174 Tal Einat. ........ r53641 | kurt.kaiser | 2007-02-06 00:02:16 +0100 (Tue, 06 Feb 2007) | 5 lines 1. Calltips now 'handle' tuples in the argument list (display '<tuple>' :) Suggested solution by Christos Georgiou, Bug 791968. 2. Clean up tests, were not failing when they should have been. 4. Remove some camelcase and an unneeded try/except block. ........ r53644 | kurt.kaiser | 2007-02-06 04:21:40 +0100 (Tue, 06 Feb 2007) | 2 lines Clean up ModifiedInterpreter.runcode() structure ........ r53646 | peter.astrand | 2007-02-06 16:37:50 +0100 (Tue, 06 Feb 2007) | 1 line Applied patch 1124861.3.patch to solve bug #1124861: Automatically create pipes on Windows, if GetStdHandle fails. Will backport. ........ r53648 | lars.gustaebel | 2007-02-06 19:38:13 +0100 (Tue, 06 Feb 2007) | 4 lines Patch #1652681: create nonexistent files in append mode and allow appending to empty files. ........ r53649 | kurt.kaiser | 2007-02-06 20:09:43 +0100 (Tue, 06 Feb 2007) | 4 lines Updated patch (CodeContext.061217.patch) to [ 1362975 ] CodeContext - Improved text indentation Tal Einat 16Dec06 ........ r53650 | kurt.kaiser | 2007-02-06 20:21:19 +0100 (Tue, 06 Feb 2007) | 2 lines narrow exception per [ 1540849 ] except too broad ........ r53653 | kurt.kaiser | 2007-02-07 04:39:41 +0100 (Wed, 07 Feb 2007) | 4 lines [ 1621265 ] Auto-completion list placement Move AC window below input line unless not enough space, then put it above. Patch: Tal Einat ........ r53654 | kurt.kaiser | 2007-02-07 09:07:13 +0100 (Wed, 07 Feb 2007) | 2 lines Handle AttributeError during calltip lookup ........ r53656 | raymond.hettinger | 2007-02-07 21:08:22 +0100 (Wed, 07 Feb 2007) | 3 lines SF #1615701: make d.update(m) honor __getitem__() and keys() in dict subclasses ........ r53658 | raymond.hettinger | 2007-02-07 22:04:20 +0100 (Wed, 07 Feb 2007) | 1 line SF: 1397711 Set docs conflated immutable and hashable ........ r53660 | raymond.hettinger | 2007-02-07 22:42:17 +0100 (Wed, 07 Feb 2007) | 1 line Check for a common user error with defaultdict(). ........ r53662 | raymond.hettinger | 2007-02-07 23:24:07 +0100 (Wed, 07 Feb 2007) | 1 line Bug #1575169: operator.isSequenceType() now returns False for subclasses of dict. ........ r53664 | raymond.hettinger | 2007-02-08 00:49:03 +0100 (Thu, 08 Feb 2007) | 1 line Silence compiler warning ........ r53666 | raymond.hettinger | 2007-02-08 01:07:32 +0100 (Thu, 08 Feb 2007) | 1 line Do not let overflows in enumerate() and count() pass silently. ........ r53668 | raymond.hettinger | 2007-02-08 01:50:39 +0100 (Thu, 08 Feb 2007) | 1 line Bypass set specific optimizations for set and frozenset subclasses. ........ r53670 | raymond.hettinger | 2007-02-08 02:42:35 +0100 (Thu, 08 Feb 2007) | 1 line Fix docstring bug ........ r53671 | martin.v.loewis | 2007-02-08 10:13:36 +0100 (Thu, 08 Feb 2007) | 3 lines Bug #1653736: Complain about keyword arguments to time.isoformat. Will backport to 2.5. ........ r53679 | kurt.kaiser | 2007-02-08 23:58:18 +0100 (Thu, 08 Feb 2007) | 6 lines Corrected some bugs in AutoComplete. Also, Page Up/Down in ACW implemented; mouse and cursor selection in ACWindow implemented; double Tab inserts current selection and closes ACW (similar to double-click and Return); scroll wheel now works in ACW. Added AutoComplete instructions to IDLE Help. ........ r53689 | martin.v.loewis | 2007-02-09 13:19:32 +0100 (Fri, 09 Feb 2007) | 3 lines Bug #1653736: Properly discard third argument to slot_nb_inplace_power. Will backport. ........ r53691 | martin.v.loewis | 2007-02-09 13:36:48 +0100 (Fri, 09 Feb 2007) | 4 lines Bug #1600860: Search for shared python library in LIBDIR, not lib/python/config, on "linux" and "gnu" systems. Will backport. ........ r53693 | martin.v.loewis | 2007-02-09 13:58:49 +0100 (Fri, 09 Feb 2007) | 2 lines Update broken link. Will backport to 2.5. ........ r53697 | georg.brandl | 2007-02-09 19:48:41 +0100 (Fri, 09 Feb 2007) | 2 lines Bug #1656078: typo in in profile docs. ........ r53731 | brett.cannon | 2007-02-11 06:36:00 +0100 (Sun, 11 Feb 2007) | 3 lines Change a very minor inconsistency (that is purely cosmetic) in the AST definition. ........ r53735 | skip.montanaro | 2007-02-11 19:24:37 +0100 (Sun, 11 Feb 2007) | 1 line fix trace.py --ignore-dir ........ r53741 | brett.cannon | 2007-02-11 20:44:41 +0100 (Sun, 11 Feb 2007) | 3 lines Check in changed Python-ast.c from a cosmetic change to Python.asdl (in r53731). ........ r53751 | brett.cannon | 2007-02-12 04:51:02 +0100 (Mon, 12 Feb 2007) | 5 lines Modify Parser/asdl_c.py so that the __version__ number for Python/Python-ast.c is specified at the top of the file. Also add a note that Python/Python-ast.c needs to be committed separately after a change to the AST grammar to capture the revision number of the change (which is what __version__ is set to). ........ r53752 | lars.gustaebel | 2007-02-12 10:25:53 +0100 (Mon, 12 Feb 2007) | 3 lines Bug #1656581: Point out that external file objects are supposed to be at position 0. ........ r53754 | martin.v.loewis | 2007-02-12 13:21:10 +0100 (Mon, 12 Feb 2007) | 3 lines Patch 1463026: Support default namespace in XMLGenerator. Fixes #847665. Will backport. ........ r53757 | armin.rigo | 2007-02-12 17:23:24 +0100 (Mon, 12 Feb 2007) | 4 lines Fix the line to what is my guess at the original author's meaning. (The line has no effect anyway, but is present because it's customary call the base class __init__). ........ r53763 | martin.v.loewis | 2007-02-13 09:34:45 +0100 (Tue, 13 Feb 2007) | 3 lines Patch #685268: Consider a package's __path__ in imputil. Will backport. ........ r53765 | martin.v.loewis | 2007-02-13 10:49:38 +0100 (Tue, 13 Feb 2007) | 2 lines Patch #698833: Support file decryption in zipfile. ........ r53766 | martin.v.loewis | 2007-02-13 11:10:39 +0100 (Tue, 13 Feb 2007) | 3 lines Patch #1517891: Make 'a' create the file if it doesn't exist. Fixes #1514451. ........ r53767 | martin.v.loewis | 2007-02-13 13:08:24 +0100 (Tue, 13 Feb 2007) | 3 lines Bug #1658794: Remove extraneous 'this'. Will backport to 2.5. ........ r53769 | martin.v.loewis | 2007-02-13 13:14:19 +0100 (Tue, 13 Feb 2007) | 3 lines Patch #1657276: Make NETLINK_DNRTMSG conditional. Will backport. ........ r53771 | lars.gustaebel | 2007-02-13 17:09:24 +0100 (Tue, 13 Feb 2007) | 4 lines Patch #1647484: Renamed GzipFile's filename attribute to name. The filename attribute is still accessible as a property that emits a DeprecationWarning. ........ r53772 | lars.gustaebel | 2007-02-13 17:24:00 +0100 (Tue, 13 Feb 2007) | 3 lines Strip the '.gz' extension from the filename that is written to the gzip header. ........ r53774 | martin.v.loewis | 2007-02-14 11:07:37 +0100 (Wed, 14 Feb 2007) | 2 lines Patch #1432399: Add HCI sockets. ........ r53775 | martin.v.loewis | 2007-02-14 12:30:07 +0100 (Wed, 14 Feb 2007) | 2 lines Update 1432399 to removal of _BT_SOCKADDR_MEMB. ........ r53776 | martin.v.loewis | 2007-02-14 12:30:56 +0100 (Wed, 14 Feb 2007) | 3 lines Ignore directory time stamps when considering whether to rerun libffi configure. ........ r53778 | lars.gustaebel | 2007-02-14 15:45:12 +0100 (Wed, 14 Feb 2007) | 4 lines A missing binary mode in AppendTest caused failures in Windows Buildbot. ........ r53782 | martin.v.loewis | 2007-02-15 10:51:35 +0100 (Thu, 15 Feb 2007) | 2 lines Patch #1397848: add the reasoning behind no-resize-on-shrinkage. ........ r53783 | georg.brandl | 2007-02-15 11:37:59 +0100 (Thu, 15 Feb 2007) | 2 lines Make functools.wraps() docs a bit clearer. ........ r53785 | georg.brandl | 2007-02-15 12:29:04 +0100 (Thu, 15 Feb 2007) | 2 lines Patch #1494140: Add documentation for the new struct.Struct object. ........ r53787 | georg.brandl | 2007-02-15 12:29:55 +0100 (Thu, 15 Feb 2007) | 2 lines Add missing \versionadded. ........ r53800 | brett.cannon | 2007-02-15 23:54:39 +0100 (Thu, 15 Feb 2007) | 11 lines Update the encoding package's search function to use absolute imports when calling __import__. This helps make the expected search locations for encoding modules be more explicit. One could use an explicit value for __path__ when making the call to __import__ to force the exact location searched for encodings. This would give the most strict search path possible if one is worried about malicious code being imported. The unfortunate side-effect of that is that if __path__ was modified on 'encodings' on purpose in a safe way it would not be picked up in future __import__ calls. ........ r53801 | brett.cannon | 2007-02-16 20:33:01 +0100 (Fri, 16 Feb 2007) | 2 lines Make the __import__ call in encodings.__init__ absolute with a level 0 call. ........ r53809 | vinay.sajip | 2007-02-16 23:36:24 +0100 (Fri, 16 Feb 2007) | 1 line Minor fix for currentframe (SF #1652788). ........ r53818 | raymond.hettinger | 2007-02-19 03:03:19 +0100 (Mon, 19 Feb 2007) | 3 lines Extend work on revision 52962: Eliminate redundant calls to PyObject_Hash(). ........ r53820 | raymond.hettinger | 2007-02-19 05:08:43 +0100 (Mon, 19 Feb 2007) | 1 line Add merge() function to heapq. ........ r53821 | raymond.hettinger | 2007-02-19 06:28:28 +0100 (Mon, 19 Feb 2007) | 1 line Add tie-breaker count to preserve sort stability. ........ r53822 | raymond.hettinger | 2007-02-19 07:59:32 +0100 (Mon, 19 Feb 2007) | 1 line Use C heapreplace() instead of slower _siftup() in pure python. ........ r53823 | raymond.hettinger | 2007-02-19 08:30:21 +0100 (Mon, 19 Feb 2007) | 1 line Add test for merge stability ........ r53824 | raymond.hettinger | 2007-02-19 10:14:10 +0100 (Mon, 19 Feb 2007) | 1 line Provide an example of defaultdict with non-zero constant factory function. ........ r53825 | lars.gustaebel | 2007-02-19 10:54:47 +0100 (Mon, 19 Feb 2007) | 2 lines Moved misplaced news item. ........ r53826 | martin.v.loewis | 2007-02-19 11:55:19 +0100 (Mon, 19 Feb 2007) | 3 lines Patch #1490190: posixmodule now includes os.chflags() and os.lchflags() functions on platforms where the underlying system calls are available. ........ r53827 | raymond.hettinger | 2007-02-19 19:15:04 +0100 (Mon, 19 Feb 2007) | 1 line Fixup docstrings for merge(). ........ r53829 | raymond.hettinger | 2007-02-19 21:44:04 +0100 (Mon, 19 Feb 2007) | 1 line Fixup set/dict interoperability. ........ r53837 | raymond.hettinger | 2007-02-21 06:20:38 +0100 (Wed, 21 Feb 2007) | 1 line Add itertools.izip_longest(). ........ r53838 | raymond.hettinger | 2007-02-21 18:22:05 +0100 (Wed, 21 Feb 2007) | 1 line Remove filler struct item and fix leak. ........
341 lines
11 KiB
TeX
341 lines
11 KiB
TeX
\section{\module{collections} ---
|
|
High-performance container datatypes}
|
|
|
|
\declaremodule{standard}{collections}
|
|
\modulesynopsis{High-performance datatypes}
|
|
\moduleauthor{Raymond Hettinger}{python@rcn.com}
|
|
\sectionauthor{Raymond Hettinger}{python@rcn.com}
|
|
\versionadded{2.4}
|
|
|
|
|
|
This module implements high-performance container datatypes. Currently,
|
|
there are two datatypes, deque and defaultdict.
|
|
Future additions may include balanced trees and ordered dictionaries.
|
|
\versionchanged[Added defaultdict]{2.5}
|
|
|
|
\subsection{\class{deque} objects \label{deque-objects}}
|
|
|
|
\begin{funcdesc}{deque}{\optional{iterable}}
|
|
Returns a new deque objected initialized left-to-right (using
|
|
\method{append()}) with data from \var{iterable}. If \var{iterable}
|
|
is not specified, the new deque is empty.
|
|
|
|
Deques are a generalization of stacks and queues (the name is pronounced
|
|
``deck'' and is short for ``double-ended queue''). Deques support
|
|
thread-safe, memory efficient appends and pops from either side of the deque
|
|
with approximately the same \code{O(1)} performance in either direction.
|
|
|
|
Though \class{list} objects support similar operations, they are optimized
|
|
for fast fixed-length operations and incur \code{O(n)} memory movement costs
|
|
for \samp{pop(0)} and \samp{insert(0, v)} operations which change both the
|
|
size and position of the underlying data representation.
|
|
\versionadded{2.4}
|
|
\end{funcdesc}
|
|
|
|
Deque objects support the following methods:
|
|
|
|
\begin{methoddesc}{append}{x}
|
|
Add \var{x} to the right side of the deque.
|
|
\end{methoddesc}
|
|
|
|
\begin{methoddesc}{appendleft}{x}
|
|
Add \var{x} to the left side of the deque.
|
|
\end{methoddesc}
|
|
|
|
\begin{methoddesc}{clear}{}
|
|
Remove all elements from the deque leaving it with length 0.
|
|
\end{methoddesc}
|
|
|
|
\begin{methoddesc}{extend}{iterable}
|
|
Extend the right side of the deque by appending elements from
|
|
the iterable argument.
|
|
\end{methoddesc}
|
|
|
|
\begin{methoddesc}{extendleft}{iterable}
|
|
Extend the left side of the deque by appending elements from
|
|
\var{iterable}. Note, the series of left appends results in
|
|
reversing the order of elements in the iterable argument.
|
|
\end{methoddesc}
|
|
|
|
\begin{methoddesc}{pop}{}
|
|
Remove and return an element from the right side of the deque.
|
|
If no elements are present, raises an \exception{IndexError}.
|
|
\end{methoddesc}
|
|
|
|
\begin{methoddesc}{popleft}{}
|
|
Remove and return an element from the left side of the deque.
|
|
If no elements are present, raises an \exception{IndexError}.
|
|
\end{methoddesc}
|
|
|
|
\begin{methoddesc}{remove}{value}
|
|
Removed the first occurrence of \var{value}. If not found,
|
|
raises a \exception{ValueError}.
|
|
\versionadded{2.5}
|
|
\end{methoddesc}
|
|
|
|
\begin{methoddesc}{rotate}{n}
|
|
Rotate the deque \var{n} steps to the right. If \var{n} is
|
|
negative, rotate to the left. Rotating one step to the right
|
|
is equivalent to: \samp{d.appendleft(d.pop())}.
|
|
\end{methoddesc}
|
|
|
|
In addition to the above, deques support iteration, pickling, \samp{len(d)},
|
|
\samp{reversed(d)}, \samp{copy.copy(d)}, \samp{copy.deepcopy(d)},
|
|
membership testing with the \keyword{in} operator, and subscript references
|
|
such as \samp{d[-1]}.
|
|
|
|
Example:
|
|
|
|
\begin{verbatim}
|
|
>>> from collections import deque
|
|
>>> d = deque('ghi') # make a new deque with three items
|
|
>>> for elem in d: # iterate over the deque's elements
|
|
... print elem.upper()
|
|
G
|
|
H
|
|
I
|
|
|
|
>>> d.append('j') # add a new entry to the right side
|
|
>>> d.appendleft('f') # add a new entry to the left side
|
|
>>> d # show the representation of the deque
|
|
deque(['f', 'g', 'h', 'i', 'j'])
|
|
|
|
>>> d.pop() # return and remove the rightmost item
|
|
'j'
|
|
>>> d.popleft() # return and remove the leftmost item
|
|
'f'
|
|
>>> list(d) # list the contents of the deque
|
|
['g', 'h', 'i']
|
|
>>> d[0] # peek at leftmost item
|
|
'g'
|
|
>>> d[-1] # peek at rightmost item
|
|
'i'
|
|
|
|
>>> list(reversed(d)) # list the contents of a deque in reverse
|
|
['i', 'h', 'g']
|
|
>>> 'h' in d # search the deque
|
|
True
|
|
>>> d.extend('jkl') # add multiple elements at once
|
|
>>> d
|
|
deque(['g', 'h', 'i', 'j', 'k', 'l'])
|
|
>>> d.rotate(1) # right rotation
|
|
>>> d
|
|
deque(['l', 'g', 'h', 'i', 'j', 'k'])
|
|
>>> d.rotate(-1) # left rotation
|
|
>>> d
|
|
deque(['g', 'h', 'i', 'j', 'k', 'l'])
|
|
|
|
>>> deque(reversed(d)) # make a new deque in reverse order
|
|
deque(['l', 'k', 'j', 'i', 'h', 'g'])
|
|
>>> d.clear() # empty the deque
|
|
>>> d.pop() # cannot pop from an empty deque
|
|
Traceback (most recent call last):
|
|
File "<pyshell#6>", line 1, in -toplevel-
|
|
d.pop()
|
|
IndexError: pop from an empty deque
|
|
|
|
>>> d.extendleft('abc') # extendleft() reverses the input order
|
|
>>> d
|
|
deque(['c', 'b', 'a'])
|
|
\end{verbatim}
|
|
|
|
\subsubsection{Recipes \label{deque-recipes}}
|
|
|
|
This section shows various approaches to working with deques.
|
|
|
|
The \method{rotate()} method provides a way to implement \class{deque}
|
|
slicing and deletion. For example, a pure python implementation of
|
|
\code{del d[n]} relies on the \method{rotate()} method to position
|
|
elements to be popped:
|
|
|
|
\begin{verbatim}
|
|
def delete_nth(d, n):
|
|
d.rotate(-n)
|
|
d.popleft()
|
|
d.rotate(n)
|
|
\end{verbatim}
|
|
|
|
To implement \class{deque} slicing, use a similar approach applying
|
|
\method{rotate()} to bring a target element to the left side of the deque.
|
|
Remove old entries with \method{popleft()}, add new entries with
|
|
\method{extend()}, and then reverse the rotation.
|
|
|
|
With minor variations on that approach, it is easy to implement Forth style
|
|
stack manipulations such as \code{dup}, \code{drop}, \code{swap}, \code{over},
|
|
\code{pick}, \code{rot}, and \code{roll}.
|
|
|
|
A roundrobin task server can be built from a \class{deque} using
|
|
\method{popleft()} to select the current task and \method{append()}
|
|
to add it back to the tasklist if the input stream is not exhausted:
|
|
|
|
\begin{verbatim}
|
|
def roundrobin(*iterables):
|
|
pending = deque(iter(i) for i in iterables)
|
|
while pending:
|
|
task = pending.popleft()
|
|
try:
|
|
yield task.next()
|
|
except StopIteration:
|
|
continue
|
|
pending.append(task)
|
|
|
|
>>> for value in roundrobin('abc', 'd', 'efgh'):
|
|
... print value
|
|
|
|
a
|
|
d
|
|
e
|
|
b
|
|
f
|
|
c
|
|
g
|
|
h
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
Multi-pass data reduction algorithms can be succinctly expressed and
|
|
efficiently coded by extracting elements with multiple calls to
|
|
\method{popleft()}, applying the reduction function, and calling
|
|
\method{append()} to add the result back to the queue.
|
|
|
|
For example, building a balanced binary tree of nested lists entails
|
|
reducing two adjacent nodes into one by grouping them in a list:
|
|
|
|
\begin{verbatim}
|
|
def maketree(iterable):
|
|
d = deque(iterable)
|
|
while len(d) > 1:
|
|
pair = [d.popleft(), d.popleft()]
|
|
d.append(pair)
|
|
return list(d)
|
|
|
|
>>> print maketree('abcdefgh')
|
|
[[[['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]]]
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
\subsection{\class{defaultdict} objects \label{defaultdict-objects}}
|
|
|
|
\begin{funcdesc}{defaultdict}{\optional{default_factory\optional{, ...}}}
|
|
Returns a new dictionary-like object. \class{defaultdict} is a subclass
|
|
of the builtin \class{dict} class. It overrides one method and adds one
|
|
writable instance variable. The remaining functionality is the same as
|
|
for the \class{dict} class and is not documented here.
|
|
|
|
The first argument provides the initial value for the
|
|
\member{default_factory} attribute; it defaults to \code{None}.
|
|
All remaining arguments are treated the same as if they were
|
|
passed to the \class{dict} constructor, including keyword arguments.
|
|
|
|
\versionadded{2.5}
|
|
\end{funcdesc}
|
|
|
|
\class{defaultdict} objects support the following method in addition to
|
|
the standard \class{dict} operations:
|
|
|
|
\begin{methoddesc}{__missing__}{key}
|
|
If the \member{default_factory} attribute is \code{None}, this raises
|
|
an \exception{KeyError} exception with the \var{key} as argument.
|
|
|
|
If \member{default_factory} is not \code{None}, it is called without
|
|
arguments to provide a default value for the given \var{key}, this
|
|
value is inserted in the dictionary for the \var{key}, and returned.
|
|
|
|
If calling \member{default_factory} raises an exception this exception
|
|
is propagated unchanged.
|
|
|
|
This method is called by the \method{__getitem__} method of the
|
|
\class{dict} class when the requested key is not found; whatever it
|
|
returns or raises is then returned or raised by \method{__getitem__}.
|
|
\end{methoddesc}
|
|
|
|
\class{defaultdict} objects support the following instance variable:
|
|
|
|
\begin{datadesc}{default_factory}
|
|
This attribute is used by the \method{__missing__} method; it is initialized
|
|
from the first argument to the constructor, if present, or to \code{None},
|
|
if absent.
|
|
\end{datadesc}
|
|
|
|
|
|
\subsubsection{\class{defaultdict} Examples \label{defaultdict-examples}}
|
|
|
|
Using \class{list} as the \member{default_factory}, it is easy to group
|
|
a sequence of key-value pairs into a dictionary of lists:
|
|
|
|
\begin{verbatim}
|
|
>>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
|
|
>>> d = defaultdict(list)
|
|
>>> for k, v in s:
|
|
d[k].append(v)
|
|
|
|
>>> d.items()
|
|
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
|
|
\end{verbatim}
|
|
|
|
When each key is encountered for the first time, it is not already in the
|
|
mapping; so an entry is automatically created using the
|
|
\member{default_factory} function which returns an empty \class{list}. The
|
|
\method{list.append()} operation then attaches the value to the new list. When
|
|
keys are encountered again, the look-up proceeds normally (returning the list
|
|
for that key) and the \method{list.append()} operation adds another value to
|
|
the list. This technique is simpler and faster than an equivalent technique
|
|
using \method{dict.setdefault()}:
|
|
|
|
\begin{verbatim}
|
|
>>> d = {}
|
|
>>> for k, v in s:
|
|
d.setdefault(k, []).append(v)
|
|
|
|
>>> d.items()
|
|
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
|
|
\end{verbatim}
|
|
|
|
Setting the \member{default_factory} to \class{int} makes the
|
|
\class{defaultdict} useful for counting (like a bag or multiset in other
|
|
languages):
|
|
|
|
\begin{verbatim}
|
|
>>> s = 'mississippi'
|
|
>>> d = defaultdict(int)
|
|
>>> for k in s:
|
|
d[k] += 1
|
|
|
|
>>> d.items()
|
|
[('i', 4), ('p', 2), ('s', 4), ('m', 1)]
|
|
\end{verbatim}
|
|
|
|
When a letter is first encountered, it is missing from the mapping, so the
|
|
\member{default_factory} function calls \function{int()} to supply a default
|
|
count of zero. The increment operation then builds up the count for each
|
|
letter.
|
|
|
|
The function \function{int()} which always returns zero is just a special
|
|
case of constant functions. A faster and more flexible way to create
|
|
constant functions is to use \function{itertools.repeat()} which can supply
|
|
any constant value (not just zero):
|
|
|
|
\begin{verbatim}
|
|
>>> def constant_factory(value):
|
|
... return itertools.repeat(value).next
|
|
>>> d = defaultdict(constant_factory('<missing>'))
|
|
>>> d.update(name='John', action='ran')
|
|
>>> '%(name)s %(action)s to %(object)s' % d
|
|
'John ran to <missing>'
|
|
\end{verbatim}
|
|
|
|
Setting the \member{default_factory} to \class{set} makes the
|
|
\class{defaultdict} useful for building a dictionary of sets:
|
|
|
|
\begin{verbatim}
|
|
>>> s = [('red', 1), ('blue', 2), ('red', 3), ('blue', 4), ('red', 1), ('blue', 4)]
|
|
>>> d = defaultdict(set)
|
|
>>> for k, v in s:
|
|
d[k].add(v)
|
|
|
|
>>> d.items()
|
|
[('blue', set([2, 4])), ('red', set([1, 3]))]
|
|
\end{verbatim}
|