blob: 1f95dd61b9fcd790391b7ba08474bacbf7f345a3 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`array` --- Efficient arrays of numeric values
2===================================================
3
4.. module:: array
Benjamin Peterson2a691a82008-03-31 01:51:45 +00005 :synopsis: Space efficient arrays of uniformly typed numeric values.
Georg Brandl116aa622007-08-15 14:28:22 +00006
Georg Brandl116aa622007-08-15 14:28:22 +00007.. index:: single: arrays
8
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04009--------------
10
Benjamin Peterson2a691a82008-03-31 01:51:45 +000011This module defines an object type which can compactly represent an array of
Georg Brandl116aa622007-08-15 14:28:22 +000012basic values: characters, integers, floating point numbers. Arrays are sequence
13types and behave very much like lists, except that the type of objects stored in
14them 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
16defined:
17
Meador Inge1c9f0c92011-09-20 19:55:51 -050018+-----------+--------------------+-------------------+-----------------------+-------+
19| Type code | C Type | Python Type | Minimum size in bytes | Notes |
20+===========+====================+===================+=======================+=======+
21| ``'b'`` | signed char | int | 1 | |
22+-----------+--------------------+-------------------+-----------------------+-------+
23| ``'B'`` | unsigned char | int | 1 | |
24+-----------+--------------------+-------------------+-----------------------+-------+
Victor Stinner62bb3942012-08-06 00:46:05 +020025| ``'u'`` | Py_UNICODE | Unicode character | 2 | \(1) |
Meador Inge1c9f0c92011-09-20 19:55:51 -050026+-----------+--------------------+-------------------+-----------------------+-------+
27| ``'h'`` | signed short | int | 2 | |
28+-----------+--------------------+-------------------+-----------------------+-------+
29| ``'H'`` | unsigned short | int | 2 | |
30+-----------+--------------------+-------------------+-----------------------+-------+
31| ``'i'`` | signed int | int | 2 | |
32+-----------+--------------------+-------------------+-----------------------+-------+
33| ``'I'`` | unsigned int | int | 2 | |
34+-----------+--------------------+-------------------+-----------------------+-------+
35| ``'l'`` | signed long | int | 4 | |
36+-----------+--------------------+-------------------+-----------------------+-------+
37| ``'L'`` | unsigned long | int | 4 | |
38+-----------+--------------------+-------------------+-----------------------+-------+
Victor Stinner62bb3942012-08-06 00:46:05 +020039| ``'q'`` | signed long long | int | 8 | \(2) |
Meador Inge1c9f0c92011-09-20 19:55:51 -050040+-----------+--------------------+-------------------+-----------------------+-------+
Victor Stinner62bb3942012-08-06 00:46:05 +020041| ``'Q'`` | unsigned long long | int | 8 | \(2) |
Meador Inge1c9f0c92011-09-20 19:55:51 -050042+-----------+--------------------+-------------------+-----------------------+-------+
43| ``'f'`` | float | float | 4 | |
44+-----------+--------------------+-------------------+-----------------------+-------+
45| ``'d'`` | double | float | 8 | |
46+-----------+--------------------+-------------------+-----------------------+-------+
Georg Brandl116aa622007-08-15 14:28:22 +000047
Meador Inge1c9f0c92011-09-20 19:55:51 -050048Notes:
Georg Brandl9cbb1252009-07-11 10:39:00 +000049
Meador Inge1c9f0c92011-09-20 19:55:51 -050050(1)
Stefan Krah029780b2012-08-24 20:14:12 +020051 The ``'u'`` type code corresponds to Python's obsolete unicode character
Victor Stinner62bb3942012-08-06 00:46:05 +020052 (:c:type:`Py_UNICODE` which is :c:type:`wchar_t`). Depending on the
53 platform, it can be 16 bits or 32 bits.
54
Stefan Krah029780b2012-08-24 20:14:12 +020055 ``'u'`` will be removed together with the rest of the :c:type:`Py_UNICODE`
56 API.
57
58 .. deprecated-removed:: 3.3 4.0
59
Victor Stinner62bb3942012-08-06 00:46:05 +020060(2)
Meador Inge1c9f0c92011-09-20 19:55:51 -050061 The ``'q'`` and ``'Q'`` type codes are available only if
Georg Brandlf0c51fa2011-09-27 07:30:00 +020062 the platform C compiler used to build Python supports C :c:type:`long long`,
63 or, on Windows, :c:type:`__int64`.
Meador Inge1c9f0c92011-09-20 19:55:51 -050064
65 .. versionadded:: 3.3
66
Georg Brandl116aa622007-08-15 14:28:22 +000067The actual representation of values is determined by the machine architecture
68(strictly speaking, by the C implementation). The actual size can be accessed
Georg Brandlba956ae2007-11-29 17:24:34 +000069through the :attr:`itemsize` attribute.
Georg Brandl116aa622007-08-15 14:28:22 +000070
71The module defines the following type:
72
73
Alexandre Vassalotti711ed4a2009-07-17 10:42:05 +000074.. class:: array(typecode[, initializer])
Georg Brandl116aa622007-08-15 14:28:22 +000075
Alexandre Vassalotti711ed4a2009-07-17 10:42:05 +000076 A new array whose items are restricted by *typecode*, and initialized
Ezio Melottic228e962013-05-04 18:06:34 +030077 from the optional *initializer* value, which must be a list, a
78 :term:`bytes-like object`, or iterable over elements of the
Guido van Rossum98297ee2007-11-06 21:34:58 +000079 appropriate type.
Georg Brandl116aa622007-08-15 14:28:22 +000080
Georg Brandl116aa622007-08-15 14:28:22 +000081 If given a list or string, the initializer is passed to the new array's
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +000082 :meth:`fromlist`, :meth:`frombytes`, or :meth:`fromunicode` method (see below)
Georg Brandl116aa622007-08-15 14:28:22 +000083 to add initial items to the array. Otherwise, the iterable initializer is
84 passed to the :meth:`extend` method.
85
Steve Dowerb82e17e2019-05-23 08:45:22 -070086 .. audit-event:: array.__new__ "typecode initializer"
Georg Brandl116aa622007-08-15 14:28:22 +000087
Guido van Rossum98297ee2007-11-06 21:34:58 +000088.. data:: typecodes
89
90 A string with all available type codes.
91
Georg Brandl116aa622007-08-15 14:28:22 +000092Array objects support the ordinary sequence operations of indexing, slicing,
93concatenation, and multiplication. When using slice assignment, the assigned
94value must be an array object with the same type code; in all other cases,
95:exc:`TypeError` is raised. Array objects also implement the buffer interface,
Serhiy Storchakae5ea1ab2016-05-18 13:54:54 +030096and may be used wherever :term:`bytes-like objects <bytes-like object>` are supported.
Georg Brandl116aa622007-08-15 14:28:22 +000097
98The following data items and methods are also supported:
99
Georg Brandl116aa622007-08-15 14:28:22 +0000100.. attribute:: array.typecode
101
102 The typecode character used to create the array.
103
104
105.. attribute:: array.itemsize
106
107 The length in bytes of one array item in the internal representation.
108
109
110.. method:: array.append(x)
111
112 Append a new item with value *x* to the end of the array.
113
114
115.. method:: array.buffer_info()
116
117 Return a tuple ``(address, length)`` giving the current memory address and the
118 length in elements of the buffer used to hold array's contents. The size of the
119 memory buffer in bytes can be computed as ``array.buffer_info()[1] *
120 array.itemsize``. This is occasionally useful when working with low-level (and
121 inherently unsafe) I/O interfaces that require memory addresses, such as certain
Georg Brandl60203b42010-10-06 10:11:56 +0000122 :c:func:`ioctl` operations. The returned numbers are valid as long as the array
Georg Brandl116aa622007-08-15 14:28:22 +0000123 exists and no length-changing operations are applied to it.
124
125 .. note::
126
127 When using array objects from code written in C or C++ (the only way to
128 effectively make use of this information), it makes more sense to use the buffer
129 interface supported by array objects. This method is maintained for backward
130 compatibility and should be avoided in new code. The buffer interface is
131 documented in :ref:`bufferobjects`.
132
133
134.. method:: array.byteswap()
135
136 "Byteswap" all items of the array. This is only supported for values which are
137 1, 2, 4, or 8 bytes in size; for other types of values, :exc:`RuntimeError` is
138 raised. It is useful when reading data from a file written on a machine with a
139 different byte order.
140
141
142.. method:: array.count(x)
143
144 Return the number of occurrences of *x* in the array.
145
146
147.. method:: array.extend(iterable)
148
149 Append items from *iterable* to the end of the array. If *iterable* is another
150 array, it must have *exactly* the same type code; if not, :exc:`TypeError` will
151 be raised. If *iterable* is not an array, it must be iterable and its elements
152 must be the right type to be appended to the array.
153
Georg Brandl116aa622007-08-15 14:28:22 +0000154
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +0000155.. method:: array.frombytes(s)
156
157 Appends items from the string, interpreting the string as an array of machine
158 values (as if it had been read from a file using the :meth:`fromfile` method).
159
160 .. versionadded:: 3.2
161 :meth:`fromstring` is renamed to :meth:`frombytes` for clarity.
162
163
Georg Brandl116aa622007-08-15 14:28:22 +0000164.. method:: array.fromfile(f, n)
165
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000166 Read *n* items (as machine values) from the :term:`file object` *f* and append
167 them to the end of the array. If less than *n* items are available,
168 :exc:`EOFError` is raised, but the items that were available are still
169 inserted into the array. *f* must be a real built-in file object; something
170 else with a :meth:`read` method won't do.
Georg Brandl116aa622007-08-15 14:28:22 +0000171
172
173.. method:: array.fromlist(list)
174
175 Append items from the list. This is equivalent to ``for x in list:
176 a.append(x)`` except that if there is a type error, the array is unchanged.
177
178
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +0000179.. method:: array.fromstring()
Georg Brandl116aa622007-08-15 14:28:22 +0000180
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +0000181 Deprecated alias for :meth:`frombytes`.
Georg Brandl116aa622007-08-15 14:28:22 +0000182
183
184.. method:: array.fromunicode(s)
185
186 Extends this array with data from the given unicode string. The array must
187 be a type ``'u'`` array; otherwise a :exc:`ValueError` is raised. Use
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +0000188 ``array.frombytes(unicodestring.encode(enc))`` to append Unicode data to an
Georg Brandl116aa622007-08-15 14:28:22 +0000189 array of some other type.
190
191
192.. method:: array.index(x)
193
194 Return the smallest *i* such that *i* is the index of the first occurrence of
195 *x* in the array.
196
197
198.. method:: array.insert(i, x)
199
200 Insert a new item with value *x* in the array before position *i*. Negative
201 values are treated as being relative to the end of the array.
202
203
204.. method:: array.pop([i])
205
206 Removes the item with the index *i* from the array and returns it. The optional
207 argument defaults to ``-1``, so that by default the last item is removed and
208 returned.
209
210
Georg Brandl116aa622007-08-15 14:28:22 +0000211.. method:: array.remove(x)
212
213 Remove the first occurrence of *x* from the array.
214
215
216.. method:: array.reverse()
217
218 Reverse the order of the items in the array.
219
220
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +0000221.. method:: array.tobytes()
222
223 Convert the array to an array of machine values and return the bytes
224 representation (the same sequence of bytes that would be written to a file by
225 the :meth:`tofile` method.)
226
227 .. versionadded:: 3.2
228 :meth:`tostring` is renamed to :meth:`tobytes` for clarity.
229
230
Georg Brandl116aa622007-08-15 14:28:22 +0000231.. method:: array.tofile(f)
232
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000233 Write all items (as machine values) to the :term:`file object` *f*.
Georg Brandl116aa622007-08-15 14:28:22 +0000234
235
236.. method:: array.tolist()
237
238 Convert the array to an ordinary list with the same items.
239
240
241.. method:: array.tostring()
242
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +0000243 Deprecated alias for :meth:`tobytes`.
Georg Brandl116aa622007-08-15 14:28:22 +0000244
245
246.. method:: array.tounicode()
247
248 Convert the array to a unicode string. The array must be a type ``'u'`` array;
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +0000249 otherwise a :exc:`ValueError` is raised. Use ``array.tobytes().decode(enc)`` to
Georg Brandl116aa622007-08-15 14:28:22 +0000250 obtain a unicode string from an array of some other type.
251
252
Georg Brandl116aa622007-08-15 14:28:22 +0000253When an array object is printed or converted to a string, it is represented as
254``array(typecode, initializer)``. The *initializer* is omitted if the array is
Georg Brandld2aa7e62008-12-06 08:12:11 +0000255empty, otherwise it is a string if the *typecode* is ``'u'``, otherwise it is a
Georg Brandl116aa622007-08-15 14:28:22 +0000256list of numbers. The string is guaranteed to be able to be converted back to an
257array with the same type and value using :func:`eval`, so long as the
Serhiy Storchakaee1b01a2016-12-02 23:13:53 +0200258:class:`~array.array` class has been imported using ``from array import array``.
Georg Brandl116aa622007-08-15 14:28:22 +0000259Examples::
260
261 array('l')
Georg Brandld2aa7e62008-12-06 08:12:11 +0000262 array('u', 'hello \u2641')
Georg Brandl116aa622007-08-15 14:28:22 +0000263 array('l', [1, 2, 3, 4, 5])
264 array('d', [1.0, 2.0, 3.14])
265
266
267.. seealso::
268
269 Module :mod:`struct`
270 Packing and unpacking of heterogeneous binary data.
271
272 Module :mod:`xdrlib`
273 Packing and unpacking of External Data Representation (XDR) data as used in some
274 remote procedure call systems.
275
Serhiy Storchaka6dff0202016-05-07 10:49:07 +0300276 `The Numerical Python Documentation <https://docs.scipy.org/doc/>`_
Georg Brandl116aa622007-08-15 14:28:22 +0000277 The Numeric Python extension (NumPy) defines another array type; see
Ezio Melottic1f58392013-06-09 01:04:21 +0300278 http://www.numpy.org/ for further information about Numerical Python.
Georg Brandl116aa622007-08-15 14:28:22 +0000279