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