Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | |
| 2 | :mod:`collections` --- High-performance container datatypes |
| 3 | =========================================================== |
| 4 | |
| 5 | .. module:: collections |
| 6 | :synopsis: High-performance datatypes |
| 7 | .. moduleauthor:: Raymond Hettinger <python@rcn.com> |
| 8 | .. sectionauthor:: Raymond Hettinger <python@rcn.com> |
| 9 | |
| 10 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 11 | This module implements high-performance container datatypes. Currently, |
| 12 | there are two datatypes, :class:`deque` and :class:`defaultdict`, and |
Raymond Hettinger | ebcee3f | 2008-02-06 19:54:00 +0000 | [diff] [blame] | 13 | one datatype factory function, :func:`namedtuple`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 14 | |
Raymond Hettinger | ebcee3f | 2008-02-06 19:54:00 +0000 | [diff] [blame] | 15 | The specialized containers provided in this module provide alternatives |
| 16 | to Python's general purpose built-in containers, :class:`dict`, |
| 17 | :class:`list`, :class:`set`, and :class:`tuple`. |
| 18 | |
| 19 | Besides the containers provided here, the optional :mod:`bsddb` |
| 20 | module offers the ability to create in-memory or file based ordered |
| 21 | dictionaries with string keys using the :meth:`bsddb.btopen` method. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 22 | |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 23 | In addition to containers, the collections module provides some ABCs |
Raymond Hettinger | ebcee3f | 2008-02-06 19:54:00 +0000 | [diff] [blame] | 24 | (abstract base classes) that can be used to test whether a class |
| 25 | provides a particular interface, for example, is it hashable or |
| 26 | a mapping. |
| 27 | |
| 28 | ABCs - abstract base classes |
| 29 | ---------------------------- |
| 30 | |
| 31 | The collections module offers the following ABCs: |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 32 | |
Mark Summerfield | 8f2d006 | 2008-02-06 13:30:44 +0000 | [diff] [blame] | 33 | ===================================== ================================================================================ |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 34 | ABC Notes |
Mark Summerfield | 8f2d006 | 2008-02-06 13:30:44 +0000 | [diff] [blame] | 35 | ===================================== ================================================================================ |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 36 | :class:`collections.Container` Defines ``__contains__()`` |
| 37 | :class:`collections.Hashable` Defines ``__hash__()`` |
| 38 | :class:`collections.Iterable` Defines ``__iter__()`` |
| 39 | :class:`collections.Iterator` Derived from :class:`Iterable` and in |
| 40 | addition defines ``__next__()`` |
Raymond Hettinger | e4c96ad | 2008-02-06 01:23:58 +0000 | [diff] [blame] | 41 | :class:`collections.Sized` Defines ``__len__()`` |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 42 | :class:`collections.Mapping` Derived from :class:`Container`, |
| 43 | :class:`Iterable`, |
| 44 | and :class:`Sized`, and in addition |
| 45 | defines ``__getitem__()``, ``get()``, |
| 46 | ``__contains__()``, ``__len__()``, |
Raymond Hettinger | e4c96ad | 2008-02-06 01:23:58 +0000 | [diff] [blame] | 47 | ``__eq__()``, ``__ne__()``, |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 48 | ``__iter__()``, ``keys()``, |
| 49 | ``items()``, and ``values()`` |
| 50 | :class:`collections.MutableMapping` Derived from :class:`Mapping` |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 51 | :class:`collections.Sequence` Derived from :class:`Container`, |
| 52 | :class:`Iterable`, and :class:`Sized`, |
| 53 | and in addition defines |
Mark Summerfield | 8f2d006 | 2008-02-06 13:30:44 +0000 | [diff] [blame] | 54 | ``__getitem__()`` |
Raymond Hettinger | e4c96ad | 2008-02-06 01:23:58 +0000 | [diff] [blame] | 55 | :class:`collections.MutableSequence` Derived from :class:`Sequence` |
Mark Summerfield | 8f2d006 | 2008-02-06 13:30:44 +0000 | [diff] [blame] | 56 | :class:`collections.Set` Derived from :class:`Container`, |
| 57 | :class:`Iterable`, and :class:`Sized`, |
Raymond Hettinger | e4c96ad | 2008-02-06 01:23:58 +0000 | [diff] [blame] | 58 | add in addition defines |
Mark Summerfield | 8f2d006 | 2008-02-06 13:30:44 +0000 | [diff] [blame] | 59 | ``__le__()``, ``__lt__()``, |
| 60 | ``__eq__()``, ``__and__()``, |
| 61 | ``__or__()``, ``__sub__()``, |
Raymond Hettinger | e4c96ad | 2008-02-06 01:23:58 +0000 | [diff] [blame] | 62 | ``__xor__()``, and ``isdisjoint()``, |
| 63 | :class:`collections.MutableSet` Derived from :class:`Set` and in |
| 64 | addition defines ``add()``, |
| 65 | ``clear()``, ``discard()``, ``pop()``, |
Mark Summerfield | 8f2d006 | 2008-02-06 13:30:44 +0000 | [diff] [blame] | 66 | ``remove()``, ``__ior__()``, |
| 67 | ``__iand__()``, ``__ixor__()``, and |
| 68 | ``__isub__()`` |
| 69 | ===================================== ================================================================================ |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 70 | |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 71 | These ABCs allow us to ask classes or instances if they provide |
| 72 | particular functionality, for example:: |
| 73 | |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 74 | size = None |
Raymond Hettinger | ebcee3f | 2008-02-06 19:54:00 +0000 | [diff] [blame] | 75 | if isinstance(myvar, collections.Sized): |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 76 | size = len(myvar) |
| 77 | |
Raymond Hettinger | ebcee3f | 2008-02-06 19:54:00 +0000 | [diff] [blame] | 78 | Several of the ABCs are also useful as mixins that make it easier to develop |
| 79 | classes supporting container APIs. For example, to write a class supporting |
| 80 | the full :class:`Set` API, it only necessary to supply the three underlying |
| 81 | abstract methods: :meth:`__contains__`, :meth:`__iter__`, and :meth:`__len__`. |
| 82 | The ABC supplies the remaining methods such as :meth:`__and__` and |
| 83 | :meth:`isdisjoint` :: |
| 84 | |
| 85 | class ListBasedSet(collections.Set): |
| 86 | 'Alternate set implementation favoring space over speed' |
| 87 | def __init__(self, iterable): |
| 88 | self.elements = list(set(iterable)) |
| 89 | def __iter__(self): |
| 90 | return iter(self.elements) |
| 91 | def __contains__(self, value): |
| 92 | return value in self.elements |
| 93 | def __len__(self): |
| 94 | return len(self.elements) |
| 95 | |
| 96 | s1 = ListBasedSet('abcdef') |
| 97 | s2 = ListBasedSet('defghi') |
| 98 | overlap = s1 & s2 # The __and__() method is supported automatically |
| 99 | |
| 100 | |
Mark Summerfield | 08898b4 | 2007-09-05 08:43:04 +0000 | [diff] [blame] | 101 | (For more about ABCs, see the :mod:`abc` module and :pep:`3119`.) |
| 102 | |
| 103 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 104 | |
| 105 | .. _deque-objects: |
| 106 | |
| 107 | :class:`deque` objects |
| 108 | ---------------------- |
| 109 | |
| 110 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 111 | .. class:: deque([iterable[, maxlen]]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 112 | |
| 113 | Returns a new deque object initialized left-to-right (using :meth:`append`) with |
| 114 | data from *iterable*. If *iterable* is not specified, the new deque is empty. |
| 115 | |
| 116 | Deques are a generalization of stacks and queues (the name is pronounced "deck" |
| 117 | and is short for "double-ended queue"). Deques support thread-safe, memory |
| 118 | efficient appends and pops from either side of the deque with approximately the |
| 119 | same O(1) performance in either direction. |
| 120 | |
| 121 | Though :class:`list` objects support similar operations, they are optimized for |
| 122 | fast fixed-length operations and incur O(n) memory movement costs for |
| 123 | ``pop(0)`` and ``insert(0, v)`` operations which change both the size and |
| 124 | position of the underlying data representation. |
| 125 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 126 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 127 | If *maxlen* is not specified or is *None*, deques may grow to an |
| 128 | arbitrary length. Otherwise, the deque is bounded to the specified maximum |
| 129 | length. Once a bounded length deque is full, when new items are added, a |
| 130 | corresponding number of items are discarded from the opposite end. Bounded |
| 131 | length deques provide functionality similar to the ``tail`` filter in |
| 132 | Unix. They are also useful for tracking transactions and other pools of data |
| 133 | where only the most recent activity is of interest. |
| 134 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 135 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 136 | Deque objects support the following methods: |
| 137 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 138 | .. method:: deque.append(x) |
| 139 | |
| 140 | Add *x* to the right side of the deque. |
| 141 | |
| 142 | |
| 143 | .. method:: deque.appendleft(x) |
| 144 | |
| 145 | Add *x* to the left side of the deque. |
| 146 | |
| 147 | |
| 148 | .. method:: deque.clear() |
| 149 | |
| 150 | Remove all elements from the deque leaving it with length 0. |
| 151 | |
| 152 | |
| 153 | .. method:: deque.extend(iterable) |
| 154 | |
| 155 | Extend the right side of the deque by appending elements from the iterable |
| 156 | argument. |
| 157 | |
| 158 | |
| 159 | .. method:: deque.extendleft(iterable) |
| 160 | |
| 161 | Extend the left side of the deque by appending elements from *iterable*. Note, |
| 162 | the series of left appends results in reversing the order of elements in the |
| 163 | iterable argument. |
| 164 | |
| 165 | |
| 166 | .. method:: deque.pop() |
| 167 | |
| 168 | Remove and return an element from the right side of the deque. If no elements |
| 169 | are present, raises an :exc:`IndexError`. |
| 170 | |
| 171 | |
| 172 | .. method:: deque.popleft() |
| 173 | |
| 174 | Remove and return an element from the left side of the deque. If no elements are |
| 175 | present, raises an :exc:`IndexError`. |
| 176 | |
| 177 | |
| 178 | .. method:: deque.remove(value) |
| 179 | |
| 180 | Removed the first occurrence of *value*. If not found, raises a |
| 181 | :exc:`ValueError`. |
| 182 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 183 | |
| 184 | .. method:: deque.rotate(n) |
| 185 | |
| 186 | Rotate the deque *n* steps to the right. If *n* is negative, rotate to the |
| 187 | left. Rotating one step to the right is equivalent to: |
| 188 | ``d.appendleft(d.pop())``. |
| 189 | |
| 190 | In addition to the above, deques support iteration, pickling, ``len(d)``, |
| 191 | ``reversed(d)``, ``copy.copy(d)``, ``copy.deepcopy(d)``, membership testing with |
| 192 | the :keyword:`in` operator, and subscript references such as ``d[-1]``. |
| 193 | |
| 194 | Example:: |
| 195 | |
| 196 | >>> from collections import deque |
| 197 | >>> d = deque('ghi') # make a new deque with three items |
| 198 | >>> for elem in d: # iterate over the deque's elements |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 199 | ... print(elem.upper()) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 200 | G |
| 201 | H |
| 202 | I |
| 203 | |
| 204 | >>> d.append('j') # add a new entry to the right side |
| 205 | >>> d.appendleft('f') # add a new entry to the left side |
| 206 | >>> d # show the representation of the deque |
| 207 | deque(['f', 'g', 'h', 'i', 'j']) |
| 208 | |
| 209 | >>> d.pop() # return and remove the rightmost item |
| 210 | 'j' |
| 211 | >>> d.popleft() # return and remove the leftmost item |
| 212 | 'f' |
| 213 | >>> list(d) # list the contents of the deque |
| 214 | ['g', 'h', 'i'] |
| 215 | >>> d[0] # peek at leftmost item |
| 216 | 'g' |
| 217 | >>> d[-1] # peek at rightmost item |
| 218 | 'i' |
| 219 | |
| 220 | >>> list(reversed(d)) # list the contents of a deque in reverse |
| 221 | ['i', 'h', 'g'] |
| 222 | >>> 'h' in d # search the deque |
| 223 | True |
| 224 | >>> d.extend('jkl') # add multiple elements at once |
| 225 | >>> d |
| 226 | deque(['g', 'h', 'i', 'j', 'k', 'l']) |
| 227 | >>> d.rotate(1) # right rotation |
| 228 | >>> d |
| 229 | deque(['l', 'g', 'h', 'i', 'j', 'k']) |
| 230 | >>> d.rotate(-1) # left rotation |
| 231 | >>> d |
| 232 | deque(['g', 'h', 'i', 'j', 'k', 'l']) |
| 233 | |
| 234 | >>> deque(reversed(d)) # make a new deque in reverse order |
| 235 | deque(['l', 'k', 'j', 'i', 'h', 'g']) |
| 236 | >>> d.clear() # empty the deque |
| 237 | >>> d.pop() # cannot pop from an empty deque |
| 238 | Traceback (most recent call last): |
| 239 | File "<pyshell#6>", line 1, in -toplevel- |
| 240 | d.pop() |
| 241 | IndexError: pop from an empty deque |
| 242 | |
| 243 | >>> d.extendleft('abc') # extendleft() reverses the input order |
| 244 | >>> d |
| 245 | deque(['c', 'b', 'a']) |
| 246 | |
| 247 | |
| 248 | .. _deque-recipes: |
| 249 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 250 | :class:`deque` Recipes |
| 251 | ^^^^^^^^^^^^^^^^^^^^^^ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 252 | |
| 253 | This section shows various approaches to working with deques. |
| 254 | |
| 255 | The :meth:`rotate` method provides a way to implement :class:`deque` slicing and |
| 256 | deletion. For example, a pure python implementation of ``del d[n]`` relies on |
| 257 | the :meth:`rotate` method to position elements to be popped:: |
| 258 | |
| 259 | def delete_nth(d, n): |
| 260 | d.rotate(-n) |
| 261 | d.popleft() |
| 262 | d.rotate(n) |
| 263 | |
| 264 | To implement :class:`deque` slicing, use a similar approach applying |
| 265 | :meth:`rotate` to bring a target element to the left side of the deque. Remove |
| 266 | old entries with :meth:`popleft`, add new entries with :meth:`extend`, and then |
| 267 | reverse the rotation. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 268 | With minor variations on that approach, it is easy to implement Forth style |
| 269 | stack manipulations such as ``dup``, ``drop``, ``swap``, ``over``, ``pick``, |
| 270 | ``rot``, and ``roll``. |
| 271 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 272 | Multi-pass data reduction algorithms can be succinctly expressed and efficiently |
| 273 | coded by extracting elements with multiple calls to :meth:`popleft`, applying |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 274 | a reduction function, and calling :meth:`append` to add the result back to the |
| 275 | deque. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 276 | |
| 277 | For example, building a balanced binary tree of nested lists entails reducing |
| 278 | two adjacent nodes into one by grouping them in a list:: |
| 279 | |
| 280 | >>> def maketree(iterable): |
| 281 | ... d = deque(iterable) |
| 282 | ... while len(d) > 1: |
| 283 | ... pair = [d.popleft(), d.popleft()] |
| 284 | ... d.append(pair) |
| 285 | ... return list(d) |
| 286 | ... |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 287 | >>> print(maketree('abcdefgh')) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 288 | [[[['a', 'b'], ['c', 'd']], [['e', 'f'], ['g', 'h']]]] |
| 289 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 290 | Bounded length deques provide functionality similar to the ``tail`` filter |
| 291 | in Unix:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 292 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 293 | def tail(filename, n=10): |
| 294 | 'Return the last n lines of a file' |
| 295 | return deque(open(filename), n) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 296 | |
| 297 | .. _defaultdict-objects: |
| 298 | |
| 299 | :class:`defaultdict` objects |
| 300 | ---------------------------- |
| 301 | |
| 302 | |
| 303 | .. class:: defaultdict([default_factory[, ...]]) |
| 304 | |
| 305 | Returns a new dictionary-like object. :class:`defaultdict` is a subclass of the |
| 306 | builtin :class:`dict` class. It overrides one method and adds one writable |
| 307 | instance variable. The remaining functionality is the same as for the |
| 308 | :class:`dict` class and is not documented here. |
| 309 | |
| 310 | The first argument provides the initial value for the :attr:`default_factory` |
| 311 | attribute; it defaults to ``None``. All remaining arguments are treated the same |
| 312 | as if they were passed to the :class:`dict` constructor, including keyword |
| 313 | arguments. |
| 314 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 315 | |
| 316 | :class:`defaultdict` objects support the following method in addition to the |
| 317 | standard :class:`dict` operations: |
| 318 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 319 | .. method:: defaultdict.__missing__(key) |
| 320 | |
| 321 | If the :attr:`default_factory` attribute is ``None``, this raises an |
| 322 | :exc:`KeyError` exception with the *key* as argument. |
| 323 | |
| 324 | If :attr:`default_factory` is not ``None``, it is called without arguments to |
| 325 | provide a default value for the given *key*, this value is inserted in the |
| 326 | dictionary for the *key*, and returned. |
| 327 | |
| 328 | If calling :attr:`default_factory` raises an exception this exception is |
| 329 | propagated unchanged. |
| 330 | |
| 331 | This method is called by the :meth:`__getitem__` method of the :class:`dict` |
| 332 | class when the requested key is not found; whatever it returns or raises is then |
| 333 | returned or raised by :meth:`__getitem__`. |
| 334 | |
| 335 | :class:`defaultdict` objects support the following instance variable: |
| 336 | |
| 337 | |
| 338 | .. attribute:: defaultdict.default_factory |
| 339 | |
| 340 | This attribute is used by the :meth:`__missing__` method; it is initialized from |
| 341 | the first argument to the constructor, if present, or to ``None``, if absent. |
| 342 | |
| 343 | |
| 344 | .. _defaultdict-examples: |
| 345 | |
| 346 | :class:`defaultdict` Examples |
| 347 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 348 | |
| 349 | Using :class:`list` as the :attr:`default_factory`, it is easy to group a |
| 350 | sequence of key-value pairs into a dictionary of lists:: |
| 351 | |
| 352 | >>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)] |
| 353 | >>> d = defaultdict(list) |
| 354 | >>> for k, v in s: |
| 355 | ... d[k].append(v) |
| 356 | ... |
| 357 | >>> d.items() |
| 358 | [('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])] |
| 359 | |
| 360 | When each key is encountered for the first time, it is not already in the |
| 361 | mapping; so an entry is automatically created using the :attr:`default_factory` |
| 362 | function which returns an empty :class:`list`. The :meth:`list.append` |
| 363 | operation then attaches the value to the new list. When keys are encountered |
| 364 | again, the look-up proceeds normally (returning the list for that key) and the |
| 365 | :meth:`list.append` operation adds another value to the list. This technique is |
| 366 | simpler and faster than an equivalent technique using :meth:`dict.setdefault`:: |
| 367 | |
| 368 | >>> d = {} |
| 369 | >>> for k, v in s: |
| 370 | ... d.setdefault(k, []).append(v) |
| 371 | ... |
| 372 | >>> d.items() |
| 373 | [('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])] |
| 374 | |
| 375 | Setting the :attr:`default_factory` to :class:`int` makes the |
| 376 | :class:`defaultdict` useful for counting (like a bag or multiset in other |
| 377 | languages):: |
| 378 | |
| 379 | >>> s = 'mississippi' |
| 380 | >>> d = defaultdict(int) |
| 381 | >>> for k in s: |
| 382 | ... d[k] += 1 |
| 383 | ... |
| 384 | >>> d.items() |
| 385 | [('i', 4), ('p', 2), ('s', 4), ('m', 1)] |
| 386 | |
| 387 | When a letter is first encountered, it is missing from the mapping, so the |
| 388 | :attr:`default_factory` function calls :func:`int` to supply a default count of |
| 389 | zero. The increment operation then builds up the count for each letter. |
| 390 | |
| 391 | The function :func:`int` which always returns zero is just a special case of |
| 392 | constant functions. A faster and more flexible way to create constant functions |
| 393 | is to use a lambda function which can supply any constant value (not just |
| 394 | zero):: |
| 395 | |
| 396 | >>> def constant_factory(value): |
| 397 | ... return lambda: value |
| 398 | >>> d = defaultdict(constant_factory('<missing>')) |
| 399 | >>> d.update(name='John', action='ran') |
| 400 | >>> '%(name)s %(action)s to %(object)s' % d |
| 401 | 'John ran to <missing>' |
| 402 | |
| 403 | Setting the :attr:`default_factory` to :class:`set` makes the |
| 404 | :class:`defaultdict` useful for building a dictionary of sets:: |
| 405 | |
| 406 | >>> s = [('red', 1), ('blue', 2), ('red', 3), ('blue', 4), ('red', 1), ('blue', 4)] |
| 407 | >>> d = defaultdict(set) |
| 408 | >>> for k, v in s: |
| 409 | ... d[k].add(v) |
| 410 | ... |
| 411 | >>> d.items() |
| 412 | [('blue', set([2, 4])), ('red', set([1, 3]))] |
| 413 | |
| 414 | |
| 415 | .. _named-tuple-factory: |
| 416 | |
Guido van Rossum | 3d392eb | 2007-11-16 00:35:22 +0000 | [diff] [blame] | 417 | :func:`namedtuple` Factory Function for Tuples with Named Fields |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 418 | ---------------------------------------------------------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 419 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 420 | Named tuples assign meaning to each position in a tuple and allow for more readable, |
| 421 | self-documenting code. They can be used wherever regular tuples are used, and |
| 422 | they add the ability to access fields by name instead of position index. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 423 | |
Guido van Rossum | 3d392eb | 2007-11-16 00:35:22 +0000 | [diff] [blame] | 424 | .. function:: namedtuple(typename, fieldnames, [verbose]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 425 | |
| 426 | Returns a new tuple subclass named *typename*. The new subclass is used to |
| 427 | create tuple-like objects that have fields accessable by attribute lookup as |
| 428 | well as being indexable and iterable. Instances of the subclass also have a |
| 429 | helpful docstring (with typename and fieldnames) and a helpful :meth:`__repr__` |
| 430 | method which lists the tuple contents in a ``name=value`` format. |
| 431 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 432 | The *fieldnames* are a single string with each fieldname separated by whitespace |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 433 | and/or commas, for example ``'x y'`` or ``'x, y'``. Alternatively, *fieldnames* |
| 434 | can be a sequence of strings such as ``['x', 'y']``. |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 435 | |
| 436 | Any valid Python identifier may be used for a fieldname except for names |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 437 | starting with an underscore. Valid identifiers consist of letters, digits, |
| 438 | and underscores but do not start with a digit or underscore and cannot be |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 439 | a :mod:`keyword` such as *class*, *for*, *return*, *global*, *pass*, |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 440 | or *raise*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 441 | |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 442 | If *verbose* is true, the class definition is printed just before being built. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 443 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 444 | Named tuple instances do not have per-instance dictionaries, so they are |
Thomas Wouters | 8ce81f7 | 2007-09-20 18:22:40 +0000 | [diff] [blame] | 445 | lightweight and require no more memory than regular tuples. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 446 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 447 | Example:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 448 | |
Guido van Rossum | 3d392eb | 2007-11-16 00:35:22 +0000 | [diff] [blame] | 449 | >>> Point = namedtuple('Point', 'x y', verbose=True) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 450 | class Point(tuple): |
| 451 | 'Point(x, y)' |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 452 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 453 | __slots__ = () |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 454 | |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 455 | _fields = ('x', 'y') |
| 456 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 457 | def __new__(cls, x, y): |
| 458 | return tuple.__new__(cls, (x, y)) |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 459 | |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 460 | @classmethod |
| 461 | def _make(cls, iterable): |
| 462 | 'Make a new Point object from a sequence or iterable' |
| 463 | result = tuple.__new__(cls, iterable) |
| 464 | if len(result) != 2: |
| 465 | raise TypeError('Expected 2 arguments, got %d' % len(result)) |
| 466 | return result |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 467 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 468 | def __repr__(self): |
| 469 | return 'Point(x=%r, y=%r)' % self |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 470 | |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 471 | def _asdict(t): |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 472 | 'Return a new dict which maps field names to their values' |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 473 | return {'x': t[0], 'y': t[1]} |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 474 | |
| 475 | def _replace(self, **kwds): |
Guido van Rossum | 3d392eb | 2007-11-16 00:35:22 +0000 | [diff] [blame] | 476 | 'Return a new Point object replacing specified fields with new values' |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 477 | result = self._make(map(kwds.pop, ('x', 'y'), self)) |
| 478 | if kwds: |
| 479 | raise ValueError('Got unexpected field names: %r' % kwds.keys()) |
| 480 | return result |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 481 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 482 | x = property(itemgetter(0)) |
| 483 | y = property(itemgetter(1)) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 484 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 485 | >>> p = Point(11, y=22) # instantiate with positional or keyword arguments |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 486 | >>> p[0] + p[1] # indexable like the plain tuple (11, 22) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 487 | 33 |
| 488 | >>> x, y = p # unpack like a regular tuple |
| 489 | >>> x, y |
| 490 | (11, 22) |
| 491 | >>> p.x + p.y # fields also accessable by name |
| 492 | 33 |
| 493 | >>> p # readable __repr__ with a name=value style |
| 494 | Point(x=11, y=22) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 495 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 496 | Named tuples are especially useful for assigning field names to result tuples returned |
| 497 | by the :mod:`csv` or :mod:`sqlite3` modules:: |
| 498 | |
Guido van Rossum | 3d392eb | 2007-11-16 00:35:22 +0000 | [diff] [blame] | 499 | EmployeeRecord = namedtuple('EmployeeRecord', 'name, age, title, department, paygrade') |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 500 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 501 | import csv |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 502 | for emp in map(EmployeeRecord._make, csv.reader(open("employees.csv", "rb"))): |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 503 | print(emp.name, emp.title) |
| 504 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 505 | import sqlite3 |
| 506 | conn = sqlite3.connect('/companydata') |
| 507 | cursor = conn.cursor() |
| 508 | cursor.execute('SELECT name, age, title, department, paygrade FROM employees') |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 509 | for emp in map(EmployeeRecord._make, cursor.fetchall()): |
Christian Heimes | 0041223 | 2008-01-10 16:02:19 +0000 | [diff] [blame] | 510 | print(emp.name, emp.title) |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 511 | |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 512 | In addition to the methods inherited from tuples, named tuples support |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 513 | three additional methods and one attribute. To prevent conflicts with |
| 514 | field names, the method and attribute names start with an underscore. |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 515 | |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 516 | .. method:: somenamedtuple._make(iterable) |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 517 | |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 518 | Class method that makes a new instance from an existing sequence or iterable. |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 519 | |
| 520 | :: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 521 | |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 522 | >>> t = [11, 22] |
| 523 | >>> Point._make(t) |
| 524 | Point(x=11, y=22) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 525 | |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 526 | .. method:: somenamedtuple._asdict() |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 527 | |
| 528 | Return a new dict which maps field names to their corresponding values: |
| 529 | |
| 530 | :: |
| 531 | |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 532 | >>> p._asdict() |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 533 | {'x': 11, 'y': 22} |
| 534 | |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 535 | .. method:: somenamedtuple._replace(kwargs) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 536 | |
Guido van Rossum | 3d392eb | 2007-11-16 00:35:22 +0000 | [diff] [blame] | 537 | Return a new instance of the named tuple replacing specified fields with new values: |
Thomas Wouters | 8ce81f7 | 2007-09-20 18:22:40 +0000 | [diff] [blame] | 538 | |
| 539 | :: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 540 | |
| 541 | >>> p = Point(x=11, y=22) |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 542 | >>> p._replace(x=33) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 543 | Point(x=33, y=22) |
| 544 | |
Guido van Rossum | 3d392eb | 2007-11-16 00:35:22 +0000 | [diff] [blame] | 545 | >>> for partnum, record in inventory.items(): |
Christian Heimes | 454f37b | 2008-01-10 00:10:02 +0000 | [diff] [blame] | 546 | ... inventory[partnum] = record._replace(price=newprices[partnum], timestamp=time.now()) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 547 | |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 548 | .. attribute:: somenamedtuple._fields |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 549 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 550 | Tuple of strings listing the field names. Useful for introspection |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 551 | and for creating new named tuple types from existing named tuples. |
Thomas Wouters | 8ce81f7 | 2007-09-20 18:22:40 +0000 | [diff] [blame] | 552 | |
| 553 | :: |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 554 | |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 555 | >>> p._fields # view the field names |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 556 | ('x', 'y') |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 557 | |
Guido van Rossum | 3d392eb | 2007-11-16 00:35:22 +0000 | [diff] [blame] | 558 | >>> Color = namedtuple('Color', 'red green blue') |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 559 | >>> Pixel = namedtuple('Pixel', Point._fields + Color._fields) |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 560 | >>> Pixel(11, 22, 128, 255, 0) |
Christian Heimes | 454f37b | 2008-01-10 00:10:02 +0000 | [diff] [blame] | 561 | Pixel(x=11, y=22, red=128, green=255, blue=0) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 562 | |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 563 | To retrieve a field whose name is stored in a string, use the :func:`getattr` |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 564 | function:: |
Christian Heimes | 0449f63 | 2007-12-15 01:27:15 +0000 | [diff] [blame] | 565 | |
| 566 | >>> getattr(p, 'x') |
| 567 | 11 |
| 568 | |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 569 | To convert a dictionary to a named tuple, use the double-star-operator [#]_:: |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 570 | |
| 571 | >>> d = {'x': 11, 'y': 22} |
| 572 | >>> Point(**d) |
| 573 | Point(x=11, y=22) |
| 574 | |
Guido van Rossum | 3d392eb | 2007-11-16 00:35:22 +0000 | [diff] [blame] | 575 | Since a named tuple is a regular Python class, it is easy to add or change |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 576 | functionality with a subclass. Here is how to add a calculated field and |
| 577 | a fixed-width print format:: |
Guido van Rossum | 3d392eb | 2007-11-16 00:35:22 +0000 | [diff] [blame] | 578 | |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 579 | >>> class Point(namedtuple('Point', 'x y')): |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 580 | ... __slots__ = () |
Christian Heimes | 454f37b | 2008-01-10 00:10:02 +0000 | [diff] [blame] | 581 | ... @property |
| 582 | ... def hypot(self): |
| 583 | ... return (self.x ** 2 + self.y ** 2) ** 0.5 |
| 584 | ... def __str__(self): |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 585 | ... return 'Point: x=%6.3f y=%6.3f hypot=%6.3f' % (self.x, self.y, self.hypot) |
Guido van Rossum | 3d392eb | 2007-11-16 00:35:22 +0000 | [diff] [blame] | 586 | |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 587 | >>> for p in Point(3, 4), Point(14, 5/7.): |
Christian Heimes | 0041223 | 2008-01-10 16:02:19 +0000 | [diff] [blame] | 588 | ... print(p) |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 589 | |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 590 | Point: x= 3.000 y= 4.000 hypot= 5.000 |
| 591 | Point: x=14.000 y= 0.714 hypot=14.018 |
Christian Heimes | 043d6f6 | 2008-01-07 17:19:16 +0000 | [diff] [blame] | 592 | |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 593 | The subclass shown above sets ``__slots__`` to an empty tuple. This keeps |
Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 594 | keep memory requirements low by preventing the creation of instance dictionaries. |
| 595 | |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 596 | |
| 597 | Subclassing is not useful for adding new, stored fields. Instead, simply |
| 598 | create a new named tuple type from the :attr:`_fields` attribute:: |
| 599 | |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 600 | >>> Point3D = namedtuple('Point3D', Point._fields + ('z',)) |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 601 | |
| 602 | Default values can be implemented by using :meth:`_replace` to |
Christian Heimes | 790c823 | 2008-01-07 21:14:23 +0000 | [diff] [blame] | 603 | customize a prototype instance:: |
Guido van Rossum | 3d392eb | 2007-11-16 00:35:22 +0000 | [diff] [blame] | 604 | |
| 605 | >>> Account = namedtuple('Account', 'owner balance transaction_count') |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 606 | >>> default_account = Account('<owner name>', 0.0, 0) |
| 607 | >>> johns_account = default_account._replace(owner='John') |
Guido van Rossum | 3d392eb | 2007-11-16 00:35:22 +0000 | [diff] [blame] | 608 | |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 609 | .. rubric:: Footnotes |
| 610 | |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 611 | .. [#] For information on the double-star-operator see |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 612 | :ref:`tut-unpacking-arguments` and :ref:`calls`. |
Raymond Hettinger | e4c96ad | 2008-02-06 01:23:58 +0000 | [diff] [blame] | 613 | |
| 614 | |
| 615 | |
| 616 | :class:`UserDict` objects |
Mark Summerfield | 8f2d006 | 2008-02-06 13:30:44 +0000 | [diff] [blame] | 617 | ------------------------- |
Raymond Hettinger | e4c96ad | 2008-02-06 01:23:58 +0000 | [diff] [blame] | 618 | |
| 619 | The class, :class:`UserDict` acts as a wrapper around dictionary objects. |
| 620 | The need for this class has been partially supplanted by the ability to |
| 621 | subclass directly from :class:`dict`; however, this class can be easier |
| 622 | to work with because the underlying dictionary is accessible as an |
| 623 | attribute. |
| 624 | |
| 625 | .. class:: UserDict([initialdata]) |
| 626 | |
| 627 | Class that simulates a dictionary. The instance's contents are kept in a |
| 628 | regular dictionary, which is accessible via the :attr:`data` attribute of |
| 629 | :class:`UserDict` instances. If *initialdata* is provided, :attr:`data` is |
| 630 | initialized with its contents; note that a reference to *initialdata* will not |
| 631 | be kept, allowing it be used for other purposes. |
| 632 | |
| 633 | In addition to supporting the methods and operations of mappings, |
Raymond Hettinger | ebcee3f | 2008-02-06 19:54:00 +0000 | [diff] [blame] | 634 | :class:`UserDict` instances provide the following attribute: |
Raymond Hettinger | e4c96ad | 2008-02-06 01:23:58 +0000 | [diff] [blame] | 635 | |
| 636 | .. attribute:: UserDict.data |
| 637 | |
| 638 | A real dictionary used to store the contents of the :class:`UserDict` class. |