blob: 3e275a262047ff3da4e5e279e4ee9f2bffafceeb [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
7
8.. index:: single: arrays
9
Benjamin Peterson2a691a82008-03-31 01:51:45 +000010This module defines an object type which can compactly represent an array of
Georg Brandl116aa622007-08-15 14:28:22 +000011basic values: characters, integers, floating point numbers. Arrays are sequence
12types and behave very much like lists, except that the type of objects stored in
13them 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
15defined:
16
Meador Inge1c9f0c92011-09-20 19:55:51 -050017+-----------+--------------------+-------------------+-----------------------+-------+
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+-----------+--------------------+-------------------+-----------------------+-------+
Ezio Melotti90bf5f12011-10-25 10:05:34 +030024| ``'u'`` | Py_UCS4 | Unicode character | 4 | |
Meador Inge1c9f0c92011-09-20 19:55:51 -050025+-----------+--------------------+-------------------+-----------------------+-------+
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+-----------+--------------------+-------------------+-----------------------+-------+
Ezio Melotti90bf5f12011-10-25 10:05:34 +030038| ``'q'`` | signed long long | int | 8 | \(1) |
Meador Inge1c9f0c92011-09-20 19:55:51 -050039+-----------+--------------------+-------------------+-----------------------+-------+
Ezio Melotti90bf5f12011-10-25 10:05:34 +030040| ``'Q'`` | unsigned long long | int | 8 | \(1) |
Meador Inge1c9f0c92011-09-20 19:55:51 -050041+-----------+--------------------+-------------------+-----------------------+-------+
42| ``'f'`` | float | float | 4 | |
43+-----------+--------------------+-------------------+-----------------------+-------+
44| ``'d'`` | double | float | 8 | |
45+-----------+--------------------+-------------------+-----------------------+-------+
Georg Brandl116aa622007-08-15 14:28:22 +000046
Meador Inge1c9f0c92011-09-20 19:55:51 -050047Notes:
Georg Brandl9cbb1252009-07-11 10:39:00 +000048
Meador Inge1c9f0c92011-09-20 19:55:51 -050049(1)
Meador Inge1c9f0c92011-09-20 19:55:51 -050050 The ``'q'`` and ``'Q'`` type codes are available only if
Georg Brandlf0c51fa2011-09-27 07:30:00 +020051 the platform C compiler used to build Python supports C :c:type:`long long`,
52 or, on Windows, :c:type:`__int64`.
Meador Inge1c9f0c92011-09-20 19:55:51 -050053
54 .. versionadded:: 3.3
55
Georg Brandl116aa622007-08-15 14:28:22 +000056The actual representation of values is determined by the machine architecture
57(strictly speaking, by the C implementation). The actual size can be accessed
Georg Brandlba956ae2007-11-29 17:24:34 +000058through the :attr:`itemsize` attribute.
Georg Brandl116aa622007-08-15 14:28:22 +000059
60The module defines the following type:
61
62
Alexandre Vassalotti711ed4a2009-07-17 10:42:05 +000063.. class:: array(typecode[, initializer])
Georg Brandl116aa622007-08-15 14:28:22 +000064
Alexandre Vassalotti711ed4a2009-07-17 10:42:05 +000065 A new array whose items are restricted by *typecode*, and initialized
Guido van Rossum98297ee2007-11-06 21:34:58 +000066 from the optional *initializer* value, which must be a list, object
Georg Brandl48310cd2009-01-03 21:18:54 +000067 supporting the buffer interface, or iterable over elements of the
Guido van Rossum98297ee2007-11-06 21:34:58 +000068 appropriate type.
Georg Brandl116aa622007-08-15 14:28:22 +000069
Georg Brandl116aa622007-08-15 14:28:22 +000070 If given a list or string, the initializer is passed to the new array's
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +000071 :meth:`fromlist`, :meth:`frombytes`, or :meth:`fromunicode` method (see below)
Georg Brandl116aa622007-08-15 14:28:22 +000072 to add initial items to the array. Otherwise, the iterable initializer is
73 passed to the :meth:`extend` method.
74
75
Guido van Rossum98297ee2007-11-06 21:34:58 +000076.. data:: typecodes
77
78 A string with all available type codes.
79
Georg Brandl116aa622007-08-15 14:28:22 +000080Array objects support the ordinary sequence operations of indexing, slicing,
81concatenation, and multiplication. When using slice assignment, the assigned
82value must be an array object with the same type code; in all other cases,
83:exc:`TypeError` is raised. Array objects also implement the buffer interface,
84and may be used wherever buffer objects are supported.
85
86The following data items and methods are also supported:
87
Georg Brandl116aa622007-08-15 14:28:22 +000088.. attribute:: array.typecode
89
90 The typecode character used to create the array.
91
92
93.. attribute:: array.itemsize
94
95 The length in bytes of one array item in the internal representation.
96
97
98.. method:: array.append(x)
99
100 Append a new item with value *x* to the end of the array.
101
102
103.. method:: array.buffer_info()
104
105 Return a tuple ``(address, length)`` giving the current memory address and the
106 length in elements of the buffer used to hold array's contents. The size of the
107 memory buffer in bytes can be computed as ``array.buffer_info()[1] *
108 array.itemsize``. This is occasionally useful when working with low-level (and
109 inherently unsafe) I/O interfaces that require memory addresses, such as certain
Georg Brandl60203b42010-10-06 10:11:56 +0000110 :c:func:`ioctl` operations. The returned numbers are valid as long as the array
Georg Brandl116aa622007-08-15 14:28:22 +0000111 exists and no length-changing operations are applied to it.
112
113 .. note::
114
115 When using array objects from code written in C or C++ (the only way to
116 effectively make use of this information), it makes more sense to use the buffer
117 interface supported by array objects. This method is maintained for backward
118 compatibility and should be avoided in new code. The buffer interface is
119 documented in :ref:`bufferobjects`.
120
121
122.. method:: array.byteswap()
123
124 "Byteswap" all items of the array. This is only supported for values which are
125 1, 2, 4, or 8 bytes in size; for other types of values, :exc:`RuntimeError` is
126 raised. It is useful when reading data from a file written on a machine with a
127 different byte order.
128
129
130.. method:: array.count(x)
131
132 Return the number of occurrences of *x* in the array.
133
134
135.. method:: array.extend(iterable)
136
137 Append items from *iterable* to the end of the array. If *iterable* is another
138 array, it must have *exactly* the same type code; if not, :exc:`TypeError` will
139 be raised. If *iterable* is not an array, it must be iterable and its elements
140 must be the right type to be appended to the array.
141
Georg Brandl116aa622007-08-15 14:28:22 +0000142
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +0000143.. method:: array.frombytes(s)
144
145 Appends items from the string, interpreting the string as an array of machine
146 values (as if it had been read from a file using the :meth:`fromfile` method).
147
148 .. versionadded:: 3.2
149 :meth:`fromstring` is renamed to :meth:`frombytes` for clarity.
150
151
Georg Brandl116aa622007-08-15 14:28:22 +0000152.. method:: array.fromfile(f, n)
153
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000154 Read *n* items (as machine values) from the :term:`file object` *f* and append
155 them to the end of the array. If less than *n* items are available,
156 :exc:`EOFError` is raised, but the items that were available are still
157 inserted into the array. *f* must be a real built-in file object; something
158 else with a :meth:`read` method won't do.
Georg Brandl116aa622007-08-15 14:28:22 +0000159
160
161.. method:: array.fromlist(list)
162
163 Append items from the list. This is equivalent to ``for x in list:
164 a.append(x)`` except that if there is a type error, the array is unchanged.
165
166
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +0000167.. method:: array.fromstring()
Georg Brandl116aa622007-08-15 14:28:22 +0000168
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +0000169 Deprecated alias for :meth:`frombytes`.
Georg Brandl116aa622007-08-15 14:28:22 +0000170
171
172.. method:: array.fromunicode(s)
173
174 Extends this array with data from the given unicode string. The array must
175 be a type ``'u'`` array; otherwise a :exc:`ValueError` is raised. Use
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +0000176 ``array.frombytes(unicodestring.encode(enc))`` to append Unicode data to an
Georg Brandl116aa622007-08-15 14:28:22 +0000177 array of some other type.
178
179
180.. method:: array.index(x)
181
182 Return the smallest *i* such that *i* is the index of the first occurrence of
183 *x* in the array.
184
185
186.. method:: array.insert(i, x)
187
188 Insert a new item with value *x* in the array before position *i*. Negative
189 values are treated as being relative to the end of the array.
190
191
192.. method:: array.pop([i])
193
194 Removes the item with the index *i* from the array and returns it. The optional
195 argument defaults to ``-1``, so that by default the last item is removed and
196 returned.
197
198
Georg Brandl116aa622007-08-15 14:28:22 +0000199.. method:: array.remove(x)
200
201 Remove the first occurrence of *x* from the array.
202
203
204.. method:: array.reverse()
205
206 Reverse the order of the items in the array.
207
208
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +0000209.. method:: array.tobytes()
210
211 Convert the array to an array of machine values and return the bytes
212 representation (the same sequence of bytes that would be written to a file by
213 the :meth:`tofile` method.)
214
215 .. versionadded:: 3.2
216 :meth:`tostring` is renamed to :meth:`tobytes` for clarity.
217
218
Georg Brandl116aa622007-08-15 14:28:22 +0000219.. method:: array.tofile(f)
220
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000221 Write all items (as machine values) to the :term:`file object` *f*.
Georg Brandl116aa622007-08-15 14:28:22 +0000222
223
224.. method:: array.tolist()
225
226 Convert the array to an ordinary list with the same items.
227
228
229.. method:: array.tostring()
230
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +0000231 Deprecated alias for :meth:`tobytes`.
Georg Brandl116aa622007-08-15 14:28:22 +0000232
233
234.. method:: array.tounicode()
235
236 Convert the array to a unicode string. The array must be a type ``'u'`` array;
Antoine Pitrou1ce3eb52010-09-01 20:29:34 +0000237 otherwise a :exc:`ValueError` is raised. Use ``array.tobytes().decode(enc)`` to
Georg Brandl116aa622007-08-15 14:28:22 +0000238 obtain a unicode string from an array of some other type.
239
240
Georg Brandl116aa622007-08-15 14:28:22 +0000241When an array object is printed or converted to a string, it is represented as
242``array(typecode, initializer)``. The *initializer* is omitted if the array is
Georg Brandld2aa7e62008-12-06 08:12:11 +0000243empty, otherwise it is a string if the *typecode* is ``'u'``, otherwise it is a
Georg Brandl116aa622007-08-15 14:28:22 +0000244list of numbers. The string is guaranteed to be able to be converted back to an
245array with the same type and value using :func:`eval`, so long as the
246:func:`array` function has been imported using ``from array import array``.
247Examples::
248
249 array('l')
Georg Brandld2aa7e62008-12-06 08:12:11 +0000250 array('u', 'hello \u2641')
Georg Brandl116aa622007-08-15 14:28:22 +0000251 array('l', [1, 2, 3, 4, 5])
252 array('d', [1.0, 2.0, 3.14])
253
254
255.. seealso::
256
257 Module :mod:`struct`
258 Packing and unpacking of heterogeneous binary data.
259
260 Module :mod:`xdrlib`
261 Packing and unpacking of External Data Representation (XDR) data as used in some
262 remote procedure call systems.
263
264 `The Numerical Python Manual <http://numpy.sourceforge.net/numdoc/HTML/numdoc.htm>`_
265 The Numeric Python extension (NumPy) defines another array type; see
266 http://numpy.sourceforge.net/ for further information about Numerical Python.
267 (A PDF version of the NumPy manual is available at
268 http://numpy.sourceforge.net/numdoc/numdoc.pdf).
269