Files
hustoj/web/python-docs/library/array.html
2025-03-09 17:10:56 +08:00

546 lines
36 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
<head>
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<title>array --- 高效的数字数组 &#8212; Python 3.8.20 文档</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/language_data.js"></script>
<script src="../_static/translations.js"></script>
<script src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="在 Python 3.8.20 文档 中搜索"
href="../_static/opensearch.xml"/>
<link rel="author" title="关于这些文档" href="../about.html" />
<link rel="index" title="索引" href="../genindex.html" />
<link rel="search" title="搜索" href="../search.html" />
<link rel="copyright" title="版权所有" href="../copyright.html" />
<link rel="next" title="weakref --- 弱引用" href="weakref.html" />
<link rel="prev" title="bisect --- 数组二分查找算法" href="bisect.html" />
<link rel="canonical" href="https://docs.python.org/3/library/array.html" />
<style>
@media only screen {
table.full-width-table {
width: 100%;
}
}
</style>
<link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/menu.js"></script>
</head>
<body>
<div class="mobile-nav">
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
<label for="menuToggler" class="toggler__label">
<span></span>
</label>
<nav class="nav-content" role="navigation">
<a href="https://www.python.org/" class="nav-logo">
<img src="../_static/py.svg" alt="Logo"/>
</a>
<div class="version_switcher_placeholder"></div>
<form role="search" class="search" action="../search.html" method="get">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
<path fill-rule="nonzero"
d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z" fill="#444"></path>
</svg>
<input type="text" name="q" aria-label="快速搜索"/>
<input type="submit" value="转向"/>
</form>
</nav>
<div class="menu-wrapper">
<nav class="menu" role="navigation" aria-label="main navigation">
<div class="language_switcher_placeholder"></div>
<h4>上一个主题</h4>
<p class="topless"><a href="bisect.html"
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">bisect</span></code> --- 数组二分查找算法</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="weakref.html"
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">weakref</span></code> --- 弱引用</a></p>
<div role="note" aria-label="source link">
<h3>本页</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">报告 Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/array.rst"
rel="nofollow">显示源代码
</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
这个文档所针对的是一个已不再受支持的 Python 旧版本。
你应当升级版本,并阅读
<a href="/3/library/array.html"> Python 当前稳定版本的文档</a>.
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>导航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="总目录"
accesskey="I">索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模块索引"
>模块</a> |</li>
<li class="right" >
<a href="weakref.html" title="weakref --- 弱引用"
accesskey="N">下一页</a> |</li>
<li class="right" >
<a href="bisect.html" title="bisect --- 数组二分查找算法"
accesskey="P">上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="datatypes.html" accesskey="U">数据类型</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="module-array">
<span id="array-efficient-arrays-of-numeric-values"></span><h1><a class="reference internal" href="#module-array" title="array: Space efficient arrays of uniformly typed numeric values."><code class="xref py py-mod docutils literal notranslate"><span class="pre">array</span></code></a> --- 高效的数字数组<a class="headerlink" href="#module-array" title="永久链接至标题"></a></h1>
<hr class="docutils" id="index-0" />
<p>此模块定义了一种对象类型,可以紧凑地表示由基本值(字符、整数、浮点数)组成的数组。数组是序列类型,其行为与列表非常相似,不同之处在于其中存储的对象类型是受限的,在数组对象创建时用单个字符的 <em class="dfn">类型码</em> 来指定。已定义的类型码如下:</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 14%" />
<col style="width: 25%" />
<col style="width: 24%" />
<col style="width: 29%" />
<col style="width: 9%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>类型码</p></th>
<th class="head"><p>C 类型</p></th>
<th class="head"><p>Python 类型</p></th>
<th class="head"><p>最小字节数</p></th>
<th class="head"><p>备注</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'b'</span></code></p></td>
<td><p>signed char</p></td>
<td><p>int</p></td>
<td><p>1</p></td>
<td></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'B'</span></code></p></td>
<td><p>unsigned char</p></td>
<td><p>int</p></td>
<td><p>1</p></td>
<td></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'u'</span></code></p></td>
<td><p>Py_UNICODE</p></td>
<td><p>Unicode 字符</p></td>
<td><p>2</p></td>
<td><p>(1)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'h'</span></code></p></td>
<td><p>signed short</p></td>
<td><p>int</p></td>
<td><p>2</p></td>
<td></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'H'</span></code></p></td>
<td><p>unsigned short</p></td>
<td><p>int</p></td>
<td><p>2</p></td>
<td></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'i'</span></code></p></td>
<td><p>signed int</p></td>
<td><p>int</p></td>
<td><p>2</p></td>
<td></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'I'</span></code></p></td>
<td><p>unsigned int</p></td>
<td><p>int</p></td>
<td><p>2</p></td>
<td></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'l'</span></code></p></td>
<td><p>signed long</p></td>
<td><p>int</p></td>
<td><p>4</p></td>
<td></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'L'</span></code></p></td>
<td><p>unsigned long</p></td>
<td><p>int</p></td>
<td><p>4</p></td>
<td></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'q'</span></code></p></td>
<td><p>signed long long</p></td>
<td><p>int</p></td>
<td><p>8</p></td>
<td></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'Q'</span></code></p></td>
<td><p>unsigned long long</p></td>
<td><p>int</p></td>
<td><p>8</p></td>
<td></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'f'</span></code></p></td>
<td><p>float</p></td>
<td><p>float</p></td>
<td><p>4</p></td>
<td></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'d'</span></code></p></td>
<td><p>double</p></td>
<td><p>float</p></td>
<td><p>8</p></td>
<td></td>
</tr>
</tbody>
</table>
<p>注释:</p>
<ol class="arabic">
<li><p><code class="docutils literal notranslate"><span class="pre">'u'</span></code> 类型码对应于 Python 中已过时的 unicode 字符 (<a class="reference internal" href="../c-api/unicode.html#c.Py_UNICODE" title="Py_UNICODE"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_UNICODE</span></code></a><code class="xref c c-type docutils literal notranslate"><span class="pre">wchar_t</span></code>)。 根据系统平台的不同,它可能是 16 位或 32 位。</p>
<p><code class="docutils literal notranslate"><span class="pre">'u'</span></code> 将与其它的 <a class="reference internal" href="../c-api/unicode.html#c.Py_UNICODE" title="Py_UNICODE"><code class="xref c c-type docutils literal notranslate"><span class="pre">Py_UNICODE</span></code></a> API 一起被移除。</p>
<div class="deprecated-removed">
<p><span class="versionmodified">Deprecated since version 3.3, will be removed in version 4.0.</span></p>
</div>
</li>
</ol>
<p>值的实际表示会由机器的架构决定(严格地说是由 C 实现决定)。 实际大小可通过 <code class="xref py py-attr docutils literal notranslate"><span class="pre">itemsize</span></code> 属性来获取。</p>
<p>此模块定义了以下类型:</p>
<dl class="class">
<dt id="array.array">
<em class="property">class </em><code class="sig-prename descclassname">array.</code><code class="sig-name descname">array</code><span class="sig-paren">(</span><em class="sig-param">typecode</em><span class="optional">[</span>, <em class="sig-param">initializer</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#array.array" title="永久链接至目标"></a></dt>
<dd><p>一个由 <em>typecode</em> 限定类型的新数组,可由可选的 <em>initializer</em> 初始化。<em>initializer</em> 必须为一个列表、<a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> 或在合适类型元素上迭代的可迭代对象。</p>
<p>如果是一个列表或字符串,该 initializer 会被传给新数组的 <a class="reference internal" href="#array.array.fromlist" title="array.array.fromlist"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromlist()</span></code></a><a class="reference internal" href="#array.array.frombytes" title="array.array.frombytes"><code class="xref py py-meth docutils literal notranslate"><span class="pre">frombytes()</span></code></a><a class="reference internal" href="#array.array.fromunicode" title="array.array.fromunicode"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromunicode()</span></code></a> 方法(见下)以将初始项添加到数组中。其它将可迭代对象将被传给 <a class="reference internal" href="#array.array.extend" title="array.array.extend"><code class="xref py py-meth docutils literal notranslate"><span class="pre">extend()</span></code></a> 方法。</p>
<p class="audit-hook">引发一个 <a class="reference internal" href="sys.html#auditing"><span class="std std-ref">审计事件</span></a> <code class="docutils literal notranslate"><span class="pre">array.__new__</span></code> 并附带参数 <code class="docutils literal notranslate"><span class="pre">typecode</span></code>, <code class="docutils literal notranslate"><span class="pre">initializer</span></code></p>
</dd></dl>
<dl class="data">
<dt id="array.typecodes">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">typecodes</code><a class="headerlink" href="#array.typecodes" title="永久链接至目标"></a></dt>
<dd><p>一个由所有可用的类型码组成的字符串。</p>
</dd></dl>
<p>数组对象支持普通的序列操作如索引、切片、拼接和重复等。当使用切片赋值时,所赋的值必须为具有相同类型码的数组对象;所有其他情况都将引发 <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>。数组对象也实现了缓冲区接口,可以用于所有支持 <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">类字节对象</span></a> 的场合。</p>
<p>以下数据项和方法也受到支持:</p>
<dl class="attribute">
<dt id="array.array.typecode">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">typecode</code><a class="headerlink" href="#array.array.typecode" title="永久链接至目标"></a></dt>
<dd><p>在创建数组时使用的类型码字符。</p>
</dd></dl>
<dl class="attribute">
<dt id="array.array.itemsize">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">itemsize</code><a class="headerlink" href="#array.array.itemsize" title="永久链接至目标"></a></dt>
<dd><p>内部表示中,单个数组项的长度。单位为字节。</p>
</dd></dl>
<dl class="method">
<dt id="array.array.append">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">append</code><span class="sig-paren">(</span><em class="sig-param">x</em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.append" title="永久链接至目标"></a></dt>
<dd><p>添加一个值为 <em>x</em> 的新项到数组末尾。</p>
</dd></dl>
<dl class="method">
<dt id="array.array.buffer_info">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">buffer_info</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#array.array.buffer_info" title="永久链接至目标"></a></dt>
<dd><p>返回一个元组 <code class="docutils literal notranslate"><span class="pre">(address,</span> <span class="pre">length)</span></code> 以给出用于存放数组内容的缓冲区元素的当前内存地址和长度。 以字节表示的内存缓冲区大小可通过 <code class="docutils literal notranslate"><span class="pre">array.buffer_info()[1]</span> <span class="pre">*</span> <span class="pre">array.itemsize</span></code> 来计算。 这在使用需要内存地址的低层级(因此不够安全) I/O 接口时会很有用,例如某些 <code class="xref c c-func docutils literal notranslate"><span class="pre">ioctl()</span></code> 操作。 只要数组存在并且没有应用改变长度的操作,返回数值就是有效的。</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>只有在使用以 C 或 C++ 编写的代码中的数组对象时,才能有效利用该信息,但此时,更合理的是,使用数组对象支持的缓冲区接口。因此,该方法的存在仅仅是为了向后兼容性,应避免在新代码中使用。缓冲区接口的文档参见 <a class="reference internal" href="../c-api/buffer.html#bufferobjects"><span class="std std-ref">缓冲协议</span></a></p>
</div>
</dd></dl>
<dl class="method">
<dt id="array.array.byteswap">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">byteswap</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#array.array.byteswap" title="永久链接至目标"></a></dt>
<dd><p>“字节对调”所有数组项。此方法只支持大小为 1, 2, 4 或 8 字节的值;对于其它类型的值将引发 <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a>。当要从另一种字节顺序的机器生成的文件中读取数据时,它很有用。</p>
</dd></dl>
<dl class="method">
<dt id="array.array.count">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">count</code><span class="sig-paren">(</span><em class="sig-param">x</em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.count" title="永久链接至目标"></a></dt>
<dd><p>返回 <em>x</em> 在数组中的出现次数。</p>
</dd></dl>
<dl class="method">
<dt id="array.array.extend">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">extend</code><span class="sig-paren">(</span><em class="sig-param">iterable</em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.extend" title="永久链接至目标"></a></dt>
<dd><p>将来自 <em>iterable</em> 的项添加到数组末尾。如果 <em>iterable</em> 是另一个数组,它必须具有 <em>完全</em> 相同的类型码;否则将引发 <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>。如果 <em>iterable</em> 不是一个数组,则它必须为可迭代对象且其元素的类型须为可添加到数组的适当类型。</p>
</dd></dl>
<dl class="method">
<dt id="array.array.frombytes">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">frombytes</code><span class="sig-paren">(</span><em class="sig-param">s</em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.frombytes" title="永久链接至目标"></a></dt>
<dd><p>将来自字节串的项添加到数组末尾,字节串被视为由机器值组成的数组(就像用 <a class="reference internal" href="#array.array.fromfile" title="array.array.fromfile"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromfile()</span></code></a> 方法从文件中读取数据一样)。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.2 新版功能: </span><a class="reference internal" href="#array.array.fromstring" title="array.array.fromstring"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fromstring()</span></code></a> 重命名为 <a class="reference internal" href="#array.array.frombytes" title="array.array.frombytes"><code class="xref py py-meth docutils literal notranslate"><span class="pre">frombytes()</span></code></a> 以使其含义更清晰。</p>
</div>
</dd></dl>
<dl class="method">
<dt id="array.array.fromfile">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">fromfile</code><span class="sig-paren">(</span><em class="sig-param">f</em>, <em class="sig-param">n</em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.fromfile" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a> <em>f</em> 中读取 <em>n</em> 项(视为机器值)并将它们添加到数组末尾。如果可用的项少于 <em>n</em> 项,则会引发 <a class="reference internal" href="exceptions.html#EOFError" title="EOFError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">EOFError</span></code></a>,但可用的项仍然会被加进数组。</p>
</dd></dl>
<dl class="method">
<dt id="array.array.fromlist">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">fromlist</code><span class="sig-paren">(</span><em class="sig-param">list</em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.fromlist" title="永久链接至目标"></a></dt>
<dd><p>将来自列表的项添加到数组末尾。等价于 <code class="docutils literal notranslate"><span class="pre">for</span> <span class="pre">x</span> <span class="pre">in</span> <span class="pre">list:</span> <span class="pre">a.append(x)</span></code>,而不同之处在于,若发生类型错误,数组则不会被改变。</p>
</dd></dl>
<dl class="method">
<dt id="array.array.fromstring">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">fromstring</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#array.array.fromstring" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="#array.array.frombytes" title="array.array.frombytes"><code class="xref py py-meth docutils literal notranslate"><span class="pre">frombytes()</span></code></a> 的已弃用别名。</p>
<div class="deprecated-removed">
<p><span class="versionmodified">Deprecated since version 3.2, will be removed in version 3.9.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="array.array.fromunicode">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">fromunicode</code><span class="sig-paren">(</span><em class="sig-param">s</em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.fromunicode" title="永久链接至目标"></a></dt>
<dd><p>将来自 Unicode 字符串的项添加到数组末尾。数组必须是类型为 <code class="docutils literal notranslate"><span class="pre">'u'</span></code> 的数组;否则将引发 <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>。请用 <code class="docutils literal notranslate"><span class="pre">array.frombytes(unicodestring.encode(enc))</span></code> 来将 Unicode 数据添加到其它类型的数组。</p>
</dd></dl>
<dl class="method">
<dt id="array.array.index">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">index</code><span class="sig-paren">(</span><em class="sig-param">x</em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.index" title="永久链接至目标"></a></dt>
<dd><p>返回最小的 <em>i</em> 使得 <em>i</em><em>x</em> 在数组中首次出现的序号。</p>
</dd></dl>
<dl class="method">
<dt id="array.array.insert">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">insert</code><span class="sig-paren">(</span><em class="sig-param">i</em>, <em class="sig-param">x</em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.insert" title="永久链接至目标"></a></dt>
<dd><p>在数组的位置 <em>i</em> 之前插入一个值为 <em>x</em> 的新项。负值被视为相对于数组末尾的位置。</p>
</dd></dl>
<dl class="method">
<dt id="array.array.pop">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">pop</code><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param">i</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#array.array.pop" title="永久链接至目标"></a></dt>
<dd><p>从数组中移除下标为 <em>i</em> 的项并将其返回。参数默认值为 <code class="docutils literal notranslate"><span class="pre">-1</span></code>,因此默认移除并返回末项。</p>
</dd></dl>
<dl class="method">
<dt id="array.array.remove">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">remove</code><span class="sig-paren">(</span><em class="sig-param">x</em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.remove" title="永久链接至目标"></a></dt>
<dd><p>从数组中移除第一个出现的 <em>x</em></p>
</dd></dl>
<dl class="method">
<dt id="array.array.reverse">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">reverse</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#array.array.reverse" title="永久链接至目标"></a></dt>
<dd><p>反转数组中各项的顺序。</p>
</dd></dl>
<dl class="method">
<dt id="array.array.tobytes">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">tobytes</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#array.array.tobytes" title="永久链接至目标"></a></dt>
<dd><p>将数组转换为一个由机器值组成的数组并返回其字节表示(和用 <a class="reference internal" href="#array.array.tofile" title="array.array.tofile"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tofile()</span></code></a> 方法写入文件的字节序列相同)。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.2 新版功能: </span><a class="reference internal" href="#array.array.tostring" title="array.array.tostring"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tostring()</span></code></a> 被重命名为 <a class="reference internal" href="#array.array.tobytes" title="array.array.tobytes"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tobytes()</span></code></a> 以使其含义更清晰。</p>
</div>
</dd></dl>
<dl class="method">
<dt id="array.array.tofile">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">tofile</code><span class="sig-paren">(</span><em class="sig-param">f</em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.tofile" title="永久链接至目标"></a></dt>
<dd><p>将所有项(作为机器值)写入 <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a> <em>f</em></p>
</dd></dl>
<dl class="method">
<dt id="array.array.tolist">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">tolist</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#array.array.tolist" title="永久链接至目标"></a></dt>
<dd><p>将数组转换为由相同的项组成的普通列表。</p>
</dd></dl>
<dl class="method">
<dt id="array.array.tostring">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">tostring</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#array.array.tostring" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="#array.array.tobytes" title="array.array.tobytes"><code class="xref py py-meth docutils literal notranslate"><span class="pre">tobytes()</span></code></a> 的已弃用别名。</p>
<div class="deprecated-removed">
<p><span class="versionmodified">Deprecated since version 3.2, will be removed in version 3.9.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="array.array.tounicode">
<code class="sig-prename descclassname">array.</code><code class="sig-name descname">tounicode</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#array.array.tounicode" title="永久链接至目标"></a></dt>
<dd><p>将数组转换为一个 Unicode 字符串。数组必须是类型为 <code class="docutils literal notranslate"><span class="pre">'u'</span></code> 的数组;否则将引发 <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>。请用 <code class="docutils literal notranslate"><span class="pre">array.tobytes().decode(enc)</span></code> 来将其它类型的数组转换为 Unicode 字符串。</p>
</dd></dl>
<p>数组对象被打印或转换为字符串时,会被表示为 <code class="docutils literal notranslate"><span class="pre">array(typecode,</span> <span class="pre">initializer)</span></code><em>initializer</em> 在数组为空时会被省略,在数组的 <em>typecode</em><code class="docutils literal notranslate"><span class="pre">'u'</span></code> 时是一个字符串,其它时候是一个数字列表。只要 <a class="reference internal" href="#array.array" title="array.array"><code class="xref py py-class docutils literal notranslate"><span class="pre">array</span></code></a> 类已用 <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">array</span> <span class="pre">import</span> <span class="pre">array</span></code> 引入,该字符串保证能被 <a class="reference internal" href="functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a> 转换回具有相同类型和值的数组。例:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">array</span><span class="p">(</span><span class="s1">&#39;l&#39;</span><span class="p">)</span>
<span class="n">array</span><span class="p">(</span><span class="s1">&#39;u&#39;</span><span class="p">,</span> <span class="s1">&#39;hello </span><span class="se">\u2641</span><span class="s1">&#39;</span><span class="p">)</span>
<span class="n">array</span><span class="p">(</span><span class="s1">&#39;l&#39;</span><span class="p">,</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">])</span>
<span class="n">array</span><span class="p">(</span><span class="s1">&#39;d&#39;</span><span class="p">,</span> <span class="p">[</span><span class="mf">1.0</span><span class="p">,</span> <span class="mf">2.0</span><span class="p">,</span> <span class="mf">3.14</span><span class="p">])</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<dl class="simple">
<dt><a class="reference internal" href="struct.html#module-struct" title="struct: Interpret bytes as packed binary data."><code class="xref py py-mod docutils literal notranslate"><span class="pre">struct</span></code></a> 模块</dt><dd><p>打包和解包异构二进制数据。</p>
</dd>
<dt><a class="reference internal" href="xdrlib.html#module-xdrlib" title="xdrlib: Encoders and decoders for the External Data Representation (XDR)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">xdrlib</span></code></a> 模块</dt><dd><p>对某些远程过程调用系统中使用的外部数据表示XDR数据进行打包和解包 。</p>
</dd>
<dt><a class="reference external" href="https://docs.scipy.org/doc/">Numerical Python 文档</a></dt><dd><p>Numeric Python 扩展 (NumPy) 定义了另一种数组类型;请访问 <a class="reference external" href="http://www.numpy.org/">http://www.numpy.org/</a> 了解有关 Numerical Python 的更多信息。</p>
</dd>
</dl>
</div>
</section>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h4>上一个主题</h4>
<p class="topless"><a href="bisect.html"
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">bisect</span></code> --- 数组二分查找算法</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="weakref.html"
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">weakref</span></code> --- 弱引用</a></p>
<div role="note" aria-label="source link">
<h3>本页</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">报告 Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/array.rst"
rel="nofollow">显示源代码
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>导航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="总目录"
>索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模块索引"
>模块</a> |</li>
<li class="right" >
<a href="weakref.html" title="weakref --- 弱引用"
>下一页</a> |</li>
<li class="right" >
<a href="bisect.html" title="bisect --- 数组二分查找算法"
>上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="datatypes.html" >数据类型</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="footer">
&copy; <a href="../copyright.html">版权所有</a> 2001-2024, Python Software Foundation.
<br />
This page is licensed under the Python Software Foundation License Version 2.
<br />
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
<br />
<br />
The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br />
<br />
最后更新于 12月 09, 2024.
<a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
<br />
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
</div>
</body>
</html>