Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v8plus / html / python / lib / module-array.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="lib.html" title='Python Library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-sets.html" />
<link rel="prev" href="module-heapq.html" />
<link rel="parent" href="misc.html" />
<link rel="next" href="module-sets.html" />
<meta name='aesop' content='information' />
<title>5.14 array -- Efficient arrays of numeric values</title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="5.13.1 Theory"
href="node196.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="5. Miscellaneous Services"
href="misc.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="5.15 sets "
href="module-sets.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="node196.html">5.13.1 Theory</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="misc.html">5. Miscellaneous Services</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-sets.html">5.15 sets </A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION0071400000000000000000">
5.14 <tt class="module">array</tt> --
Efficient arrays of numeric values</A>
</H1>
<P>
<A NAME="module-array"></A>
<P>
This module defines an object type which can efficiently represent
an array of basic values: characters, integers, floating point
numbers. Arrays<a id='l2h-1378' xml:id='l2h-1378'></a> are sequence types and behave very much
like lists, except that the type of objects stored in them is
constrained. The type is specified at object creation time by using a
<i class="dfn">type code</i>, which is a single character. The following type
codes are defined:
<P>
<div class="center"><table class="realtable">
<thead>
<tr>
<th class="center">Type code</th>
<th class="left" >C Type</th>
<th class="left" >Python Type</th>
<th class="center">Minimum size in bytes</th>
</tr>
</thead>
<tbody>
<tr><td class="center" valign="baseline"><code>'c'</code></td>
<td class="left" >char</td>
<td class="left" >character</td>
<td class="center">1</td></tr>
<tr><td class="center" valign="baseline"><code>'b'</code></td>
<td class="left" >signed char</td>
<td class="left" >int</td>
<td class="center">1</td></tr>
<tr><td class="center" valign="baseline"><code>'B'</code></td>
<td class="left" >unsigned char</td>
<td class="left" >int</td>
<td class="center">1</td></tr>
<tr><td class="center" valign="baseline"><code>'u'</code></td>
<td class="left" >Py_UNICODE</td>
<td class="left" >Unicode character</td>
<td class="center">2</td></tr>
<tr><td class="center" valign="baseline"><code>'h'</code></td>
<td class="left" >signed short</td>
<td class="left" >int</td>
<td class="center">2</td></tr>
<tr><td class="center" valign="baseline"><code>'H'</code></td>
<td class="left" >unsigned short</td>
<td class="left" >int</td>
<td class="center">2</td></tr>
<tr><td class="center" valign="baseline"><code>'i'</code></td>
<td class="left" >signed int</td>
<td class="left" >int</td>
<td class="center">2</td></tr>
<tr><td class="center" valign="baseline"><code>'I'</code></td>
<td class="left" >unsigned int</td>
<td class="left" >long</td>
<td class="center">2</td></tr>
<tr><td class="center" valign="baseline"><code>'l'</code></td>
<td class="left" >signed long</td>
<td class="left" >int</td>
<td class="center">4</td></tr>
<tr><td class="center" valign="baseline"><code>'L'</code></td>
<td class="left" >unsigned long</td>
<td class="left" >long</td>
<td class="center">4</td></tr>
<tr><td class="center" valign="baseline"><code>'f'</code></td>
<td class="left" >float</td>
<td class="left" >float</td>
<td class="center">4</td></tr>
<tr><td class="center" valign="baseline"><code>'d'</code></td>
<td class="left" >double</td>
<td class="left" >float</td>
<td class="center">8</td></tr></tbody>
</table></div>
<P>
The actual representation of values is determined by the machine
architecture (strictly speaking, by the C implementation). The actual
size can be accessed through the <tt class="member">itemsize</tt> attribute. The values
stored for <code>'L'</code> and <code>'I'</code> items will be represented as
Python long integers when retrieved, because Python's plain integer
type cannot represent the full range of C's unsigned (long) integers.
<P>
The module defines the following type:
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1354' xml:id='l2h-1354' class="function">array</tt></b>(</nobr></td>
<td><var>typecode</var><big>[</big><var>, initializer</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Return a new array whose items are restricted by <var>typecode</var>,
and initialized from the optional <var>initializer</var> value, which
must be a list, string, or iterable over elements of the
appropriate type.
<span class="versionnote">Changed in version 2.4:
Formerly, only lists or strings were accepted.</span>
If given a list or string, the initializer is passed to the
new array's <tt class="method">fromlist()</tt>, <tt class="method">fromstring()</tt>, or
<tt class="method">fromunicode()</tt> method (see below) to add initial items to
the array. Otherwise, the iterable initializer is passed to the
<tt class="method">extend()</tt> method.
</dl>
<P>
<dl><dt><b><tt id='l2h-1355' xml:id='l2h-1355'>ArrayType</tt></b></dt>
<dd>
Obsolete alias for <tt class="function">array</tt>.
</dd></dl>
<P>
Array objects support the ordinary sequence operations of
indexing, slicing, concatenation, and multiplication. When using
slice assignment, the assigned value must be an array object with the
same type code; in all other cases, <tt class="exception">TypeError</tt> is raised.
Array objects also implement the buffer interface, and may be used
wherever buffer objects are supported.
<P>
The following data items and methods are also supported:
<P>
<dl><dt><b><tt id='l2h-1356' xml:id='l2h-1356' class="member">typecode</tt></b></dt>
<dd>
The typecode character used to create the array.
</dl>
<P>
<dl><dt><b><tt id='l2h-1357' xml:id='l2h-1357' class="member">itemsize</tt></b></dt>
<dd>
The length in bytes of one array item in the internal representation.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1358' xml:id='l2h-1358' class="method">append</tt></b>(</nobr></td>
<td><var>x</var>)</td></tr></table></dt>
<dd>
Append a new item with value <var>x</var> to the end of the array.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1359' xml:id='l2h-1359' class="method">buffer_info</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Return a tuple <code>(<var>address</var>, <var>length</var>)</code> giving the current
memory address and the length in elements of the buffer used to hold
array's contents. The size of the memory buffer in bytes can be
computed as <code><var>array</var>.buffer_info()[1] *
<var>array</var>.itemsize</code>. This is occasionally useful when working with
low-level (and inherently unsafe) I/O interfaces that require memory
addresses, such as certain <tt class="cfunction">ioctl()</tt> operations. The
returned numbers are valid as long as the array exists and no
length-changing operations are applied to it.
<P>
<span class="note"><b class="label">Note:</b>
When using array objects from code written in C or
C++ (the only way to effectively make use of this information), it
makes more sense to use the buffer interface supported by array
objects. This method is maintained for backward compatibility and
should be avoided in new code. The buffer interface is documented in
the <em class="citetitle"><a
href="../api/newTypes.html"
title="Python/C API Reference Manual"
>Python/C API Reference Manual</a></em>.</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1360' xml:id='l2h-1360' class="method">byteswap</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
``Byteswap'' all items of the array. This is only supported for
values which are 1, 2, 4, or 8 bytes in size; for other types of
values, <tt class="exception">RuntimeError</tt> is raised. It is useful when reading
data from a file written on a machine with a different byte order.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1361' xml:id='l2h-1361' class="method">count</tt></b>(</nobr></td>
<td><var>x</var>)</td></tr></table></dt>
<dd>
Return the number of occurrences of <var>x</var> in the array.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1362' xml:id='l2h-1362' class="method">extend</tt></b>(</nobr></td>
<td><var>iterable</var>)</td></tr></table></dt>
<dd>
Append items from <var>iterable</var> to the end of the array. If
<var>iterable</var> is another array, it must have <em>exactly</em> the same
type code; if not, <tt class="exception">TypeError</tt> will be raised. If
<var>iterable</var> is not an array, it must be iterable and its
elements must be the right type to be appended to the array.
<span class="versionnote">Changed in version 2.4:
Formerly, the argument could only be another array.</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1363' xml:id='l2h-1363' class="method">fromfile</tt></b>(</nobr></td>
<td><var>f, n</var>)</td></tr></table></dt>
<dd>
Read <var>n</var> items (as machine values) from the file object <var>f</var>
and append them to the end of the array. If less than <var>n</var> items
are available, <tt class="exception">EOFError</tt> is raised, but the items that were
available are still inserted into the array. <var>f</var> must be a real
built-in file object; something else with a <tt class="method">read()</tt> method won't
do.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1364' xml:id='l2h-1364' class="method">fromlist</tt></b>(</nobr></td>
<td><var>list</var>)</td></tr></table></dt>
<dd>
Append items from the list. This is equivalent to
"<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.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1365' xml:id='l2h-1365' class="method">fromstring</tt></b>(</nobr></td>
<td><var>s</var>)</td></tr></table></dt>
<dd>
Appends items from the string, interpreting the string as an
array of machine values (as if it had been read from a
file using the <tt class="method">fromfile()</tt> method).
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1366' xml:id='l2h-1366' class="method">fromunicode</tt></b>(</nobr></td>
<td><var>s</var>)</td></tr></table></dt>
<dd>
Extends this array with data from the given unicode string.
The array must be a type 'u' array; otherwise a ValueError
is raised. Use "<tt class="samp">array.fromstring(ustr.decode(enc))</tt>" to
append Unicode data to an array of some other type.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1367' xml:id='l2h-1367' class="method">index</tt></b>(</nobr></td>
<td><var>x</var>)</td></tr></table></dt>
<dd>
Return the smallest <var>i</var> such that <var>i</var> is the index of
the first occurrence of <var>x</var> in the array.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1368' xml:id='l2h-1368' class="method">insert</tt></b>(</nobr></td>
<td><var>i, x</var>)</td></tr></table></dt>
<dd>
Insert a new item with value <var>x</var> in the array before position
<var>i</var>. Negative values are treated as being relative to the end
of the array.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1369' xml:id='l2h-1369' class="method">pop</tt></b>(</nobr></td>
<td><var></var><big>[</big><var>i</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Removes the item with the index <var>i</var> from the array and returns
it. The optional argument defaults to <code>-1</code>, so that by default
the last item is removed and returned.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1370' xml:id='l2h-1370' class="method">read</tt></b>(</nobr></td>
<td><var>f, n</var>)</td></tr></table></dt>
<dd>
<div class="versionnote"><b>Deprecated since release 1.5.1.</b>
Use the <tt class="method">fromfile()</tt> method.</div><p></p>
Read <var>n</var> items (as machine values) from the file object <var>f</var>
and append them to the end of the array. If less than <var>n</var> items
are available, <tt class="exception">EOFError</tt> is raised, but the items that were
available are still inserted into the array. <var>f</var> must be a real
built-in file object; something else with a <tt class="method">read()</tt> method won't
do.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1371' xml:id='l2h-1371' class="method">remove</tt></b>(</nobr></td>
<td><var>x</var>)</td></tr></table></dt>
<dd>
Remove the first occurrence of <var>x</var> from the array.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1372' xml:id='l2h-1372' class="method">reverse</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Reverse the order of the items in the array.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1373' xml:id='l2h-1373' class="method">tofile</tt></b>(</nobr></td>
<td><var>f</var>)</td></tr></table></dt>
<dd>
Write all items (as machine values) to the file object <var>f</var>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1374' xml:id='l2h-1374' class="method">tolist</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Convert the array to an ordinary list with the same items.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1375' xml:id='l2h-1375' class="method">tostring</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Convert the array to an array of machine values and return the
string representation (the same sequence of bytes that would
be written to a file by the <tt class="method">tofile()</tt> method.)
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1376' xml:id='l2h-1376' class="method">tounicode</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Convert the array to a unicode string. The array must be
a type 'u' array; otherwise a ValueError is raised. Use
array.tostring().decode(enc) to obtain a unicode string
from an array of some other type.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1377' xml:id='l2h-1377' class="method">write</tt></b>(</nobr></td>
<td><var>f</var>)</td></tr></table></dt>
<dd>
<div class="versionnote"><b>Deprecated since release 1.5.1.</b>
Use the <tt class="method">tofile()</tt> method.</div><p></p>
Write all items (as machine values) to the file object <var>f</var>.
</dl>
<P>
When an array object is printed or converted to a string, it is
represented as <code>array(<var>typecode</var>, <var>initializer</var>)</code>. The
<var>initializer</var> is omitted if the array is empty, otherwise it is a
string if the <var>typecode</var> is <code>'c'</code>, otherwise it is a list of
numbers. The string is guaranteed to be able to be converted back to
an array with the same type and value using reverse quotes
(<code>``</code>), so long as the <tt class="function">array()</tt> function has been
imported using <code>from array import array</code>. Examples:
<P>
<div class="verbatim"><pre>
array('l')
array('c', 'hello world')
array('u', u'hello \textbackslash u2641')
array('l', [1, 2, 3, 4, 5])
array('d', [1.0, 2.0, 3.14])
</pre></div>
<P>
<div class="seealso">
<p class="heading">See Also:</p>
<dl compact="compact" class="seemodule">
<dt>Module <b><tt class="module"><a href="module-struct.html">struct</a></tt>:</b>
<dd>Packing and unpacking of heterogeneous binary data.
</dl>
<dl compact="compact" class="seemodule">
<dt>Module <b><tt class="module"><a href="module-xdrlib.html">xdrlib</a></tt>:</b>
<dd>Packing and unpacking of External Data
Representation (XDR) data as used in some remote
procedure call systems.
</dl>
<dl compact="compact" class="seetitle">
<dt><em class="citetitle"><a href="http://numpy.sourceforge.net/numdoc/HTML/numdoc.htm"
>The
Numerical Python Manual</a></em></dt>
<dd>The Numeric Python extension
(NumPy) defines another array type; see
<a class="url" href="http://numpy.sourceforge.net/">http://numpy.sourceforge.net/</a> for further information
about Numerical Python. (A PDF version of the NumPy manual
is available at
<a class="url" href="http://numpy.sourceforge.net/numdoc/numdoc.pdf">http://numpy.sourceforge.net/numdoc/numdoc.pdf</a>).</dd>
</dl>
</div>
<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="5.13.1 Theory"
href="node196.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="5. Miscellaneous Services"
href="misc.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="5.15 sets "
href="module-sets.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="node196.html">5.13.1 Theory</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="misc.html">5. Miscellaneous Services</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-sets.html">5.15 sets </A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>