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