Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / src / nas,5.n2.os.2 / lib / python / html / python / lib / module-array.html
CommitLineData
86530b38
AT
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<html>
3<head>
4<link rel="STYLESHEET" href="lib.css" type='text/css' />
5<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
6<link rel='start' href='../index.html' title='Python Documentation Index' />
7<link rel="first" href="lib.html" title='Python Library Reference' />
8<link rel='contents' href='contents.html' title="Contents" />
9<link rel='index' href='genindex.html' title='Index' />
10<link rel='last' href='about.html' title='About this document...' />
11<link rel='help' href='about.html' title='About this document...' />
12<link rel="next" href="module-sets.html" />
13<link rel="prev" href="module-heapq.html" />
14<link rel="parent" href="misc.html" />
15<link rel="next" href="module-sets.html" />
16<meta name='aesop' content='information' />
17<title>5.14 array -- Efficient arrays of numeric values</title>
18</head>
19<body>
20<DIV CLASS="navigation">
21<div id='top-navigation-panel' xml:id='top-navigation-panel'>
22<table align="center" width="100%" cellpadding="0" cellspacing="2">
23<tr>
24<td class='online-navigation'><a rel="prev" title="5.13.1 Theory"
25 href="node196.html"><img src='../icons/previous.png'
26 border='0' height='32' alt='Previous Page' width='32' /></A></td>
27<td class='online-navigation'><a rel="parent" title="5. Miscellaneous Services"
28 href="misc.html"><img src='../icons/up.png'
29 border='0' height='32' alt='Up One Level' width='32' /></A></td>
30<td class='online-navigation'><a rel="next" title="5.15 sets "
31 href="module-sets.html"><img src='../icons/next.png'
32 border='0' height='32' alt='Next Page' width='32' /></A></td>
33<td align="center" width="100%">Python Library Reference</td>
34<td class='online-navigation'><a rel="contents" title="Table of Contents"
35 href="contents.html"><img src='../icons/contents.png'
36 border='0' height='32' alt='Contents' width='32' /></A></td>
37<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
38 border='0' height='32' alt='Module Index' width='32' /></a></td>
39<td class='online-navigation'><a rel="index" title="Index"
40 href="genindex.html"><img src='../icons/index.png'
41 border='0' height='32' alt='Index' width='32' /></A></td>
42</tr></table>
43<div class='online-navigation'>
44<b class="navlabel">Previous:</b>
45<a class="sectref" rel="prev" href="node196.html">5.13.1 Theory</A>
46<b class="navlabel">Up:</b>
47<a class="sectref" rel="parent" href="misc.html">5. Miscellaneous Services</A>
48<b class="navlabel">Next:</b>
49<a class="sectref" rel="next" href="module-sets.html">5.15 sets </A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54
55<H1><A NAME="SECTION0071400000000000000000">
565.14 <tt class="module">array</tt> --
57 Efficient arrays of numeric values</A>
58</H1>
59
60<P>
61<A NAME="module-array"></A>
62
63<P>
64This module defines an object type which can efficiently represent
65an array of basic values: characters, integers, floating point
66numbers. Arrays<a id='l2h-1378' xml:id='l2h-1378'></a> are sequence types and behave very much
67like lists, except that the type of objects stored in them is
68constrained. The type is specified at object creation time by using a
69<i class="dfn">type code</i>, which is a single character. The following type
70codes are defined:
71
72<P>
73<div class="center"><table class="realtable">
74 <thead>
75 <tr>
76 <th class="center">Type code</th>
77 <th class="left" >C Type</th>
78 <th class="left" >Python Type</th>
79 <th class="center">Minimum size in bytes</th>
80 </tr>
81 </thead>
82 <tbody>
83 <tr><td class="center" valign="baseline"><code>'c'</code></td>
84 <td class="left" >char</td>
85 <td class="left" >character</td>
86 <td class="center">1</td></tr>
87 <tr><td class="center" valign="baseline"><code>'b'</code></td>
88 <td class="left" >signed char</td>
89 <td class="left" >int</td>
90 <td class="center">1</td></tr>
91 <tr><td class="center" valign="baseline"><code>'B'</code></td>
92 <td class="left" >unsigned char</td>
93 <td class="left" >int</td>
94 <td class="center">1</td></tr>
95 <tr><td class="center" valign="baseline"><code>'u'</code></td>
96 <td class="left" >Py_UNICODE</td>
97 <td class="left" >Unicode character</td>
98 <td class="center">2</td></tr>
99 <tr><td class="center" valign="baseline"><code>'h'</code></td>
100 <td class="left" >signed short</td>
101 <td class="left" >int</td>
102 <td class="center">2</td></tr>
103 <tr><td class="center" valign="baseline"><code>'H'</code></td>
104 <td class="left" >unsigned short</td>
105 <td class="left" >int</td>
106 <td class="center">2</td></tr>
107 <tr><td class="center" valign="baseline"><code>'i'</code></td>
108 <td class="left" >signed int</td>
109 <td class="left" >int</td>
110 <td class="center">2</td></tr>
111 <tr><td class="center" valign="baseline"><code>'I'</code></td>
112 <td class="left" >unsigned int</td>
113 <td class="left" >long</td>
114 <td class="center">2</td></tr>
115 <tr><td class="center" valign="baseline"><code>'l'</code></td>
116 <td class="left" >signed long</td>
117 <td class="left" >int</td>
118 <td class="center">4</td></tr>
119 <tr><td class="center" valign="baseline"><code>'L'</code></td>
120 <td class="left" >unsigned long</td>
121 <td class="left" >long</td>
122 <td class="center">4</td></tr>
123 <tr><td class="center" valign="baseline"><code>'f'</code></td>
124 <td class="left" >float</td>
125 <td class="left" >float</td>
126 <td class="center">4</td></tr>
127 <tr><td class="center" valign="baseline"><code>'d'</code></td>
128 <td class="left" >double</td>
129 <td class="left" >float</td>
130 <td class="center">8</td></tr></tbody>
131</table></div>
132
133<P>
134The actual representation of values is determined by the machine
135architecture (strictly speaking, by the C implementation). The actual
136size can be accessed through the <tt class="member">itemsize</tt> attribute. The values
137stored for <code>'L'</code> and <code>'I'</code> items will be represented as
138Python long integers when retrieved, because Python's plain integer
139type cannot represent the full range of C's unsigned (long) integers.
140
141<P>
142The module defines the following type:
143
144<P>
145<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
146 <td><nobr><b><tt id='l2h-1354' xml:id='l2h-1354' class="function">array</tt></b>(</nobr></td>
147 <td><var>typecode</var><big>[</big><var>, initializer</var><big>]</big><var></var>)</td></tr></table></dt>
148<dd>
149Return a new array whose items are restricted by <var>typecode</var>,
150and initialized from the optional <var>initializer</var> value, which
151must be a list, string, or iterable over elements of the
152appropriate type.
153
154<span class="versionnote">Changed in version 2.4:
155Formerly, only lists or strings were accepted.</span>
156
157If given a list or string, the initializer is passed to the
158new array's <tt class="method">fromlist()</tt>, <tt class="method">fromstring()</tt>, or
159<tt class="method">fromunicode()</tt> method (see below) to add initial items to
160the array. Otherwise, the iterable initializer is passed to the
161<tt class="method">extend()</tt> method.
162</dl>
163
164<P>
165<dl><dt><b><tt id='l2h-1355' xml:id='l2h-1355'>ArrayType</tt></b></dt>
166<dd>
167Obsolete alias for <tt class="function">array</tt>.
168</dd></dl>
169
170<P>
171Array objects support the ordinary sequence operations of
172indexing, slicing, concatenation, and multiplication. When using
173slice assignment, the assigned value must be an array object with the
174same type code; in all other cases, <tt class="exception">TypeError</tt> is raised.
175Array objects also implement the buffer interface, and may be used
176wherever buffer objects are supported.
177
178<P>
179The following data items and methods are also supported:
180
181<P>
182<dl><dt><b><tt id='l2h-1356' xml:id='l2h-1356' class="member">typecode</tt></b></dt>
183<dd>
184The typecode character used to create the array.
185</dl>
186
187<P>
188<dl><dt><b><tt id='l2h-1357' xml:id='l2h-1357' class="member">itemsize</tt></b></dt>
189<dd>
190The length in bytes of one array item in the internal representation.
191</dl>
192
193<P>
194<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
195 <td><nobr><b><tt id='l2h-1358' xml:id='l2h-1358' class="method">append</tt></b>(</nobr></td>
196 <td><var>x</var>)</td></tr></table></dt>
197<dd>
198Append a new item with value <var>x</var> to the end of the array.
199</dl>
200
201<P>
202<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
203 <td><nobr><b><tt id='l2h-1359' xml:id='l2h-1359' class="method">buffer_info</tt></b>(</nobr></td>
204 <td><var></var>)</td></tr></table></dt>
205<dd>
206Return a tuple <code>(<var>address</var>, <var>length</var>)</code> giving the current
207memory address and the length in elements of the buffer used to hold
208array's contents. The size of the memory buffer in bytes can be
209computed as <code><var>array</var>.buffer_info()[1] *
210<var>array</var>.itemsize</code>. This is occasionally useful when working with
211low-level (and inherently unsafe) I/O interfaces that require memory
212addresses, such as certain <tt class="cfunction">ioctl()</tt> operations. The
213returned numbers are valid as long as the array exists and no
214length-changing operations are applied to it.
215
216<P>
217<span class="note"><b class="label">Note:</b>
218When using array objects from code written in C or
219C++ (the only way to effectively make use of this information), it
220makes more sense to use the buffer interface supported by array
221objects. This method is maintained for backward compatibility and
222should be avoided in new code. The buffer interface is documented in
223the <em class="citetitle"><a
224 href="../api/newTypes.html"
225 title="Python/C API Reference Manual"
226 >Python/C API Reference Manual</a></em>.</span>
227</dl>
228
229<P>
230<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
231 <td><nobr><b><tt id='l2h-1360' xml:id='l2h-1360' class="method">byteswap</tt></b>(</nobr></td>
232 <td><var></var>)</td></tr></table></dt>
233<dd>
234``Byteswap'' all items of the array. This is only supported for
235values which are 1, 2, 4, or 8 bytes in size; for other types of
236values, <tt class="exception">RuntimeError</tt> is raised. It is useful when reading
237data from a file written on a machine with a different byte order.
238</dl>
239
240<P>
241<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
242 <td><nobr><b><tt id='l2h-1361' xml:id='l2h-1361' class="method">count</tt></b>(</nobr></td>
243 <td><var>x</var>)</td></tr></table></dt>
244<dd>
245Return the number of occurrences of <var>x</var> in the array.
246</dl>
247
248<P>
249<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
250 <td><nobr><b><tt id='l2h-1362' xml:id='l2h-1362' class="method">extend</tt></b>(</nobr></td>
251 <td><var>iterable</var>)</td></tr></table></dt>
252<dd>
253Append items from <var>iterable</var> to the end of the array. If
254<var>iterable</var> is another array, it must have <em>exactly</em> the same
255type code; if not, <tt class="exception">TypeError</tt> will be raised. If
256<var>iterable</var> is not an array, it must be iterable and its
257elements must be the right type to be appended to the array.
258
259<span class="versionnote">Changed in version 2.4:
260Formerly, the argument could only be another array.</span>
261
262</dl>
263
264<P>
265<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
266 <td><nobr><b><tt id='l2h-1363' xml:id='l2h-1363' class="method">fromfile</tt></b>(</nobr></td>
267 <td><var>f, n</var>)</td></tr></table></dt>
268<dd>
269Read <var>n</var> items (as machine values) from the file object <var>f</var>
270and append them to the end of the array. If less than <var>n</var> items
271are available, <tt class="exception">EOFError</tt> is raised, but the items that were
272available are still inserted into the array. <var>f</var> must be a real
273built-in file object; something else with a <tt class="method">read()</tt> method won't
274do.
275</dl>
276
277<P>
278<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
279 <td><nobr><b><tt id='l2h-1364' xml:id='l2h-1364' class="method">fromlist</tt></b>(</nobr></td>
280 <td><var>list</var>)</td></tr></table></dt>
281<dd>
282Append items from the list. This is equivalent to
283"<tt class="samp">for x in <var>list</var>: a.append(x)</tt>"except that if there is a type error, the array is unchanged.
284</dl>
285
286<P>
287<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
288 <td><nobr><b><tt id='l2h-1365' xml:id='l2h-1365' class="method">fromstring</tt></b>(</nobr></td>
289 <td><var>s</var>)</td></tr></table></dt>
290<dd>
291Appends items from the string, interpreting the string as an
292array of machine values (as if it had been read from a
293file using the <tt class="method">fromfile()</tt> method).
294</dl>
295
296<P>
297<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
298 <td><nobr><b><tt id='l2h-1366' xml:id='l2h-1366' class="method">fromunicode</tt></b>(</nobr></td>
299 <td><var>s</var>)</td></tr></table></dt>
300<dd>
301Extends this array with data from the given unicode string.
302The array must be a type 'u' array; otherwise a ValueError
303is raised. Use "<tt class="samp">array.fromstring(ustr.decode(enc))</tt>" to
304append Unicode data to an array of some other type.
305</dl>
306
307<P>
308<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
309 <td><nobr><b><tt id='l2h-1367' xml:id='l2h-1367' class="method">index</tt></b>(</nobr></td>
310 <td><var>x</var>)</td></tr></table></dt>
311<dd>
312Return the smallest <var>i</var> such that <var>i</var> is the index of
313the first occurrence of <var>x</var> in the array.
314</dl>
315
316<P>
317<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
318 <td><nobr><b><tt id='l2h-1368' xml:id='l2h-1368' class="method">insert</tt></b>(</nobr></td>
319 <td><var>i, x</var>)</td></tr></table></dt>
320<dd>
321Insert a new item with value <var>x</var> in the array before position
322<var>i</var>. Negative values are treated as being relative to the end
323of the array.
324</dl>
325
326<P>
327<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
328 <td><nobr><b><tt id='l2h-1369' xml:id='l2h-1369' class="method">pop</tt></b>(</nobr></td>
329 <td><var></var><big>[</big><var>i</var><big>]</big><var></var>)</td></tr></table></dt>
330<dd>
331Removes the item with the index <var>i</var> from the array and returns
332it. The optional argument defaults to <code>-1</code>, so that by default
333the last item is removed and returned.
334</dl>
335
336<P>
337<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
338 <td><nobr><b><tt id='l2h-1370' xml:id='l2h-1370' class="method">read</tt></b>(</nobr></td>
339 <td><var>f, n</var>)</td></tr></table></dt>
340<dd>
341<div class="versionnote"><b>Deprecated since release 1.5.1.</b>
342Use the <tt class="method">fromfile()</tt> method.</div><p></p>
343Read <var>n</var> items (as machine values) from the file object <var>f</var>
344and append them to the end of the array. If less than <var>n</var> items
345are available, <tt class="exception">EOFError</tt> is raised, but the items that were
346available are still inserted into the array. <var>f</var> must be a real
347built-in file object; something else with a <tt class="method">read()</tt> method won't
348do.
349</dl>
350
351<P>
352<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
353 <td><nobr><b><tt id='l2h-1371' xml:id='l2h-1371' class="method">remove</tt></b>(</nobr></td>
354 <td><var>x</var>)</td></tr></table></dt>
355<dd>
356Remove the first occurrence of <var>x</var> from the array.
357</dl>
358
359<P>
360<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
361 <td><nobr><b><tt id='l2h-1372' xml:id='l2h-1372' class="method">reverse</tt></b>(</nobr></td>
362 <td><var></var>)</td></tr></table></dt>
363<dd>
364Reverse the order of the items in the array.
365</dl>
366
367<P>
368<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
369 <td><nobr><b><tt id='l2h-1373' xml:id='l2h-1373' class="method">tofile</tt></b>(</nobr></td>
370 <td><var>f</var>)</td></tr></table></dt>
371<dd>
372Write all items (as machine values) to the file object <var>f</var>.
373</dl>
374
375<P>
376<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
377 <td><nobr><b><tt id='l2h-1374' xml:id='l2h-1374' class="method">tolist</tt></b>(</nobr></td>
378 <td><var></var>)</td></tr></table></dt>
379<dd>
380Convert the array to an ordinary list with the same items.
381</dl>
382
383<P>
384<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
385 <td><nobr><b><tt id='l2h-1375' xml:id='l2h-1375' class="method">tostring</tt></b>(</nobr></td>
386 <td><var></var>)</td></tr></table></dt>
387<dd>
388Convert the array to an array of machine values and return the
389string representation (the same sequence of bytes that would
390be written to a file by the <tt class="method">tofile()</tt> method.)
391</dl>
392
393<P>
394<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
395 <td><nobr><b><tt id='l2h-1376' xml:id='l2h-1376' class="method">tounicode</tt></b>(</nobr></td>
396 <td><var></var>)</td></tr></table></dt>
397<dd>
398Convert the array to a unicode string. The array must be
399a type 'u' array; otherwise a ValueError is raised. Use
400array.tostring().decode(enc) to obtain a unicode string
401from an array of some other type.
402</dl>
403
404<P>
405<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
406 <td><nobr><b><tt id='l2h-1377' xml:id='l2h-1377' class="method">write</tt></b>(</nobr></td>
407 <td><var>f</var>)</td></tr></table></dt>
408<dd>
409<div class="versionnote"><b>Deprecated since release 1.5.1.</b>
410Use the <tt class="method">tofile()</tt> method.</div><p></p>
411Write all items (as machine values) to the file object <var>f</var>.
412</dl>
413
414<P>
415When an array object is printed or converted to a string, it is
416represented as <code>array(<var>typecode</var>, <var>initializer</var>)</code>. The
417<var>initializer</var> is omitted if the array is empty, otherwise it is a
418string if the <var>typecode</var> is <code>'c'</code>, otherwise it is a list of
419numbers. The string is guaranteed to be able to be converted back to
420an array with the same type and value using reverse quotes
421(<code>``</code>), so long as the <tt class="function">array()</tt> function has been
422imported using <code>from array import array</code>. Examples:
423
424<P>
425<div class="verbatim"><pre>
426array('l')
427array('c', 'hello world')
428array('u', u'hello \textbackslash u2641')
429array('l', [1, 2, 3, 4, 5])
430array('d', [1.0, 2.0, 3.14])
431</pre></div>
432
433<P>
434<div class="seealso">
435 <p class="heading">See Also:</p>
436
437 <dl compact="compact" class="seemodule">
438 <dt>Module <b><tt class="module"><a href="module-struct.html">struct</a></tt>:</b>
439 <dd>Packing and unpacking of heterogeneous binary data.
440 </dl>
441 <dl compact="compact" class="seemodule">
442 <dt>Module <b><tt class="module"><a href="module-xdrlib.html">xdrlib</a></tt>:</b>
443 <dd>Packing and unpacking of External Data
444 Representation (XDR) data as used in some remote
445 procedure call systems.
446 </dl>
447 <dl compact="compact" class="seetitle">
448 <dt><em class="citetitle"><a href="http://numpy.sourceforge.net/numdoc/HTML/numdoc.htm"
449 >The
450 Numerical Python Manual</a></em></dt>
451 <dd>The Numeric Python extension
452 (NumPy) defines another array type; see
453 <a class="url" href="http://numpy.sourceforge.net/">http://numpy.sourceforge.net/</a> for further information
454 about Numerical Python. (A PDF version of the NumPy manual
455 is available at
456 <a class="url" href="http://numpy.sourceforge.net/numdoc/numdoc.pdf">http://numpy.sourceforge.net/numdoc/numdoc.pdf</a>).</dd>
457 </dl>
458</div>
459
460<DIV CLASS="navigation">
461<div class='online-navigation'>
462<p></p><hr />
463<table align="center" width="100%" cellpadding="0" cellspacing="2">
464<tr>
465<td class='online-navigation'><a rel="prev" title="5.13.1 Theory"
466 href="node196.html"><img src='../icons/previous.png'
467 border='0' height='32' alt='Previous Page' width='32' /></A></td>
468<td class='online-navigation'><a rel="parent" title="5. Miscellaneous Services"
469 href="misc.html"><img src='../icons/up.png'
470 border='0' height='32' alt='Up One Level' width='32' /></A></td>
471<td class='online-navigation'><a rel="next" title="5.15 sets "
472 href="module-sets.html"><img src='../icons/next.png'
473 border='0' height='32' alt='Next Page' width='32' /></A></td>
474<td align="center" width="100%">Python Library Reference</td>
475<td class='online-navigation'><a rel="contents" title="Table of Contents"
476 href="contents.html"><img src='../icons/contents.png'
477 border='0' height='32' alt='Contents' width='32' /></A></td>
478<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
479 border='0' height='32' alt='Module Index' width='32' /></a></td>
480<td class='online-navigation'><a rel="index" title="Index"
481 href="genindex.html"><img src='../icons/index.png'
482 border='0' height='32' alt='Index' width='32' /></A></td>
483</tr></table>
484<div class='online-navigation'>
485<b class="navlabel">Previous:</b>
486<a class="sectref" rel="prev" href="node196.html">5.13.1 Theory</A>
487<b class="navlabel">Up:</b>
488<a class="sectref" rel="parent" href="misc.html">5. Miscellaneous Services</A>
489<b class="navlabel">Next:</b>
490<a class="sectref" rel="next" href="module-sets.html">5.15 sets </A>
491</div>
492</div>
493<hr />
494<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
495</DIV>
496<!--End of Navigation Panel-->
497<ADDRESS>
498See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
499</ADDRESS>
500</BODY>
501</HTML>