Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`array` --- Efficient arrays of numeric values |
| 2 | =================================================== |
| 3 | |
| 4 | .. module:: array |
Benjamin Peterson | 2a691a8 | 2008-03-31 01:51:45 +0000 | [diff] [blame] | 5 | :synopsis: Space efficient arrays of uniformly typed numeric values. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 6 | |
| 7 | |
| 8 | .. index:: single: arrays |
| 9 | |
Benjamin Peterson | 2a691a8 | 2008-03-31 01:51:45 +0000 | [diff] [blame] | 10 | This module defines an object type which can compactly represent an array of |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 11 | basic values: characters, integers, floating point numbers. Arrays are sequence |
| 12 | types and behave very much like lists, except that the type of objects stored in |
| 13 | them is constrained. The type is specified at object creation time by using a |
| 14 | :dfn:`type code`, which is a single character. The following type codes are |
| 15 | defined: |
| 16 | |
Meador Inge | 1c9f0c9 | 2011-09-20 19:55:51 -0500 | [diff] [blame] | 17 | +-----------+--------------------+-------------------+-----------------------+-------+ |
| 18 | | Type code | C Type | Python Type | Minimum size in bytes | Notes | |
| 19 | +===========+====================+===================+=======================+=======+ |
| 20 | | ``'b'`` | signed char | int | 1 | | |
| 21 | +-----------+--------------------+-------------------+-----------------------+-------+ |
| 22 | | ``'B'`` | unsigned char | int | 1 | | |
| 23 | +-----------+--------------------+-------------------+-----------------------+-------+ |
| 24 | | ``'u'`` | Py_UNICODE | Unicode character | 2 | \(1) | |
| 25 | +-----------+--------------------+-------------------+-----------------------+-------+ |
| 26 | | ``'h'`` | signed short | int | 2 | | |
| 27 | +-----------+--------------------+-------------------+-----------------------+-------+ |
| 28 | | ``'H'`` | unsigned short | int | 2 | | |
| 29 | +-----------+--------------------+-------------------+-----------------------+-------+ |
| 30 | | ``'i'`` | signed int | int | 2 | | |
| 31 | +-----------+--------------------+-------------------+-----------------------+-------+ |
| 32 | | ``'I'`` | unsigned int | int | 2 | | |
| 33 | +-----------+--------------------+-------------------+-----------------------+-------+ |
| 34 | | ``'l'`` | signed long | int | 4 | | |
| 35 | +-----------+--------------------+-------------------+-----------------------+-------+ |
| 36 | | ``'L'`` | unsigned long | int | 4 | | |
| 37 | +-----------+--------------------+-------------------+-----------------------+-------+ |
| 38 | | ``'q'`` | signed long long | int | 8 | \(2) | |
| 39 | +-----------+--------------------+-------------------+-----------------------+-------+ |
| 40 | | ``'Q'`` | unsigned long long | int | 8 | \(2) | |
| 41 | +-----------+--------------------+-------------------+-----------------------+-------+ |
| 42 | | ``'f'`` | float | float | 4 | | |
| 43 | +-----------+--------------------+-------------------+-----------------------+-------+ |
| 44 | | ``'d'`` | double | float | 8 | | |
| 45 | +-----------+--------------------+-------------------+-----------------------+-------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 46 | |
Meador Inge | 1c9f0c9 | 2011-09-20 19:55:51 -0500 | [diff] [blame] | 47 | Notes: |
Georg Brandl | 9cbb125 | 2009-07-11 10:39:00 +0000 | [diff] [blame] | 48 | |
Meador Inge | 1c9f0c9 | 2011-09-20 19:55:51 -0500 | [diff] [blame] | 49 | (1) |
| 50 | The ``'u'`` type code corresponds to Python's unicode character. On narrow |
Georg Brandl | 9cbb125 | 2009-07-11 10:39:00 +0000 | [diff] [blame] | 51 | Unicode builds this is 2-bytes, on wide builds this is 4-bytes. |
| 52 | |
Meador Inge | 1c9f0c9 | 2011-09-20 19:55:51 -0500 | [diff] [blame] | 53 | (2) |
| 54 | The ``'q'`` and ``'Q'`` type codes are available only if |
Georg Brandl | f0c51fa | 2011-09-27 07:30:00 +0200 | [diff] [blame] | 55 | the platform C compiler used to build Python supports C :c:type:`long long`, |
| 56 | or, on Windows, :c:type:`__int64`. |
Meador Inge | 1c9f0c9 | 2011-09-20 19:55:51 -0500 | [diff] [blame] | 57 | |
| 58 | .. versionadded:: 3.3 |
| 59 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 60 | The actual representation of values is determined by the machine architecture |
| 61 | (strictly speaking, by the C implementation). The actual size can be accessed |
Georg Brandl | ba956ae | 2007-11-29 17:24:34 +0000 | [diff] [blame] | 62 | through the :attr:`itemsize` attribute. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 63 | |
| 64 | The module defines the following type: |
| 65 | |
| 66 | |
Alexandre Vassalotti | 711ed4a | 2009-07-17 10:42:05 +0000 | [diff] [blame] | 67 | .. class:: array(typecode[, initializer]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 68 | |
Alexandre Vassalotti | 711ed4a | 2009-07-17 10:42:05 +0000 | [diff] [blame] | 69 | A new array whose items are restricted by *typecode*, and initialized |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 70 | from the optional *initializer* value, which must be a list, object |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 71 | supporting the buffer interface, or iterable over elements of the |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 72 | appropriate type. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 73 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 74 | If given a list or string, the initializer is passed to the new array's |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 75 | :meth:`fromlist`, :meth:`frombytes`, or :meth:`fromunicode` method (see below) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 76 | to add initial items to the array. Otherwise, the iterable initializer is |
| 77 | passed to the :meth:`extend` method. |
| 78 | |
| 79 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 80 | .. data:: typecodes |
| 81 | |
| 82 | A string with all available type codes. |
| 83 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 84 | Array objects support the ordinary sequence operations of indexing, slicing, |
| 85 | concatenation, and multiplication. When using slice assignment, the assigned |
| 86 | value must be an array object with the same type code; in all other cases, |
| 87 | :exc:`TypeError` is raised. Array objects also implement the buffer interface, |
| 88 | and may be used wherever buffer objects are supported. |
| 89 | |
| 90 | The following data items and methods are also supported: |
| 91 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 92 | .. attribute:: array.typecode |
| 93 | |
| 94 | The typecode character used to create the array. |
| 95 | |
| 96 | |
| 97 | .. attribute:: array.itemsize |
| 98 | |
| 99 | The length in bytes of one array item in the internal representation. |
| 100 | |
| 101 | |
| 102 | .. method:: array.append(x) |
| 103 | |
| 104 | Append a new item with value *x* to the end of the array. |
| 105 | |
| 106 | |
| 107 | .. method:: array.buffer_info() |
| 108 | |
| 109 | Return a tuple ``(address, length)`` giving the current memory address and the |
| 110 | length in elements of the buffer used to hold array's contents. The size of the |
| 111 | memory buffer in bytes can be computed as ``array.buffer_info()[1] * |
| 112 | array.itemsize``. This is occasionally useful when working with low-level (and |
| 113 | inherently unsafe) I/O interfaces that require memory addresses, such as certain |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 114 | :c:func:`ioctl` operations. The returned numbers are valid as long as the array |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 115 | exists and no length-changing operations are applied to it. |
| 116 | |
| 117 | .. note:: |
| 118 | |
| 119 | When using array objects from code written in C or C++ (the only way to |
| 120 | effectively make use of this information), it makes more sense to use the buffer |
| 121 | interface supported by array objects. This method is maintained for backward |
| 122 | compatibility and should be avoided in new code. The buffer interface is |
| 123 | documented in :ref:`bufferobjects`. |
| 124 | |
| 125 | |
| 126 | .. method:: array.byteswap() |
| 127 | |
| 128 | "Byteswap" all items of the array. This is only supported for values which are |
| 129 | 1, 2, 4, or 8 bytes in size; for other types of values, :exc:`RuntimeError` is |
| 130 | raised. It is useful when reading data from a file written on a machine with a |
| 131 | different byte order. |
| 132 | |
| 133 | |
| 134 | .. method:: array.count(x) |
| 135 | |
| 136 | Return the number of occurrences of *x* in the array. |
| 137 | |
| 138 | |
| 139 | .. method:: array.extend(iterable) |
| 140 | |
| 141 | Append items from *iterable* to the end of the array. If *iterable* is another |
| 142 | array, it must have *exactly* the same type code; if not, :exc:`TypeError` will |
| 143 | be raised. If *iterable* is not an array, it must be iterable and its elements |
| 144 | must be the right type to be appended to the array. |
| 145 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 146 | |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 147 | .. method:: array.frombytes(s) |
| 148 | |
| 149 | Appends items from the string, interpreting the string as an array of machine |
| 150 | values (as if it had been read from a file using the :meth:`fromfile` method). |
| 151 | |
| 152 | .. versionadded:: 3.2 |
| 153 | :meth:`fromstring` is renamed to :meth:`frombytes` for clarity. |
| 154 | |
| 155 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 156 | .. method:: array.fromfile(f, n) |
| 157 | |
Antoine Pitrou | 11cb961 | 2010-09-15 11:11:28 +0000 | [diff] [blame] | 158 | Read *n* items (as machine values) from the :term:`file object` *f* and append |
| 159 | them to the end of the array. If less than *n* items are available, |
| 160 | :exc:`EOFError` is raised, but the items that were available are still |
| 161 | inserted into the array. *f* must be a real built-in file object; something |
| 162 | else with a :meth:`read` method won't do. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 163 | |
| 164 | |
| 165 | .. method:: array.fromlist(list) |
| 166 | |
| 167 | Append items from the list. This is equivalent to ``for x in list: |
| 168 | a.append(x)`` except that if there is a type error, the array is unchanged. |
| 169 | |
| 170 | |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 171 | .. method:: array.fromstring() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 172 | |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 173 | Deprecated alias for :meth:`frombytes`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 174 | |
| 175 | |
| 176 | .. method:: array.fromunicode(s) |
| 177 | |
| 178 | Extends this array with data from the given unicode string. The array must |
| 179 | be a type ``'u'`` array; otherwise a :exc:`ValueError` is raised. Use |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 180 | ``array.frombytes(unicodestring.encode(enc))`` to append Unicode data to an |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 181 | array of some other type. |
| 182 | |
| 183 | |
| 184 | .. method:: array.index(x) |
| 185 | |
| 186 | Return the smallest *i* such that *i* is the index of the first occurrence of |
| 187 | *x* in the array. |
| 188 | |
| 189 | |
| 190 | .. method:: array.insert(i, x) |
| 191 | |
| 192 | Insert a new item with value *x* in the array before position *i*. Negative |
| 193 | values are treated as being relative to the end of the array. |
| 194 | |
| 195 | |
| 196 | .. method:: array.pop([i]) |
| 197 | |
| 198 | Removes the item with the index *i* from the array and returns it. The optional |
| 199 | argument defaults to ``-1``, so that by default the last item is removed and |
| 200 | returned. |
| 201 | |
| 202 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 203 | .. method:: array.remove(x) |
| 204 | |
| 205 | Remove the first occurrence of *x* from the array. |
| 206 | |
| 207 | |
| 208 | .. method:: array.reverse() |
| 209 | |
| 210 | Reverse the order of the items in the array. |
| 211 | |
| 212 | |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 213 | .. method:: array.tobytes() |
| 214 | |
| 215 | Convert the array to an array of machine values and return the bytes |
| 216 | representation (the same sequence of bytes that would be written to a file by |
| 217 | the :meth:`tofile` method.) |
| 218 | |
| 219 | .. versionadded:: 3.2 |
| 220 | :meth:`tostring` is renamed to :meth:`tobytes` for clarity. |
| 221 | |
| 222 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 223 | .. method:: array.tofile(f) |
| 224 | |
Antoine Pitrou | 11cb961 | 2010-09-15 11:11:28 +0000 | [diff] [blame] | 225 | Write all items (as machine values) to the :term:`file object` *f*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 226 | |
| 227 | |
| 228 | .. method:: array.tolist() |
| 229 | |
| 230 | Convert the array to an ordinary list with the same items. |
| 231 | |
| 232 | |
| 233 | .. method:: array.tostring() |
| 234 | |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 235 | Deprecated alias for :meth:`tobytes`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 236 | |
| 237 | |
| 238 | .. method:: array.tounicode() |
| 239 | |
| 240 | Convert the array to a unicode string. The array must be a type ``'u'`` array; |
Antoine Pitrou | 1ce3eb5 | 2010-09-01 20:29:34 +0000 | [diff] [blame] | 241 | otherwise a :exc:`ValueError` is raised. Use ``array.tobytes().decode(enc)`` to |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 242 | obtain a unicode string from an array of some other type. |
| 243 | |
| 244 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 245 | When an array object is printed or converted to a string, it is represented as |
| 246 | ``array(typecode, initializer)``. The *initializer* is omitted if the array is |
Georg Brandl | d2aa7e6 | 2008-12-06 08:12:11 +0000 | [diff] [blame] | 247 | empty, otherwise it is a string if the *typecode* is ``'u'``, otherwise it is a |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 248 | list of numbers. The string is guaranteed to be able to be converted back to an |
| 249 | array with the same type and value using :func:`eval`, so long as the |
| 250 | :func:`array` function has been imported using ``from array import array``. |
| 251 | Examples:: |
| 252 | |
| 253 | array('l') |
Georg Brandl | d2aa7e6 | 2008-12-06 08:12:11 +0000 | [diff] [blame] | 254 | array('u', 'hello \u2641') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 255 | array('l', [1, 2, 3, 4, 5]) |
| 256 | array('d', [1.0, 2.0, 3.14]) |
| 257 | |
| 258 | |
| 259 | .. seealso:: |
| 260 | |
| 261 | Module :mod:`struct` |
| 262 | Packing and unpacking of heterogeneous binary data. |
| 263 | |
| 264 | Module :mod:`xdrlib` |
| 265 | Packing and unpacking of External Data Representation (XDR) data as used in some |
| 266 | remote procedure call systems. |
| 267 | |
| 268 | `The Numerical Python Manual <http://numpy.sourceforge.net/numdoc/HTML/numdoc.htm>`_ |
| 269 | The Numeric Python extension (NumPy) defines another array type; see |
| 270 | http://numpy.sourceforge.net/ for further information about Numerical Python. |
| 271 | (A PDF version of the NumPy manual is available at |
| 272 | http://numpy.sourceforge.net/numdoc/numdoc.pdf). |
| 273 | |