-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.html
More file actions
475 lines (431 loc) · 33 KB
/
array.html
File metadata and controls
475 lines (431 loc) · 33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_TW">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>8.7. array — 高效率的數值型態陣列 — Python 3.7.0 說明文件</title>
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="../_static/translations.js"></script>
<script type="text/javascript" src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="在 Python 3.7.0 說明文件 中搜尋"
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="Copyright" href="../copyright.html" />
<link rel="next" title="8.8. weakref — Weak references" href="weakref.html" />
<link rel="prev" title="8.6. bisect — 陣列二分演算法 (Array bisection algorithm)" href="bisect.html" />
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
<link rel="canonical" href="https://docs.python.org/3/library/array.html" />
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/switchers.js"></script>
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>瀏覽</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模組索引"
>模組</a> |</li>
<li class="right" >
<a href="weakref.html" title="8.8. weakref — Weak references"
accesskey="N">下一頁</a> |</li>
<li class="right" >
<a href="bisect.html" title="8.6. bisect — 陣列二分演算法 (Array bisection algorithm)"
accesskey="P">上一頁</a> |</li>
<li><img src="../_static/py.png" alt=""
style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> »</li>
<li>
<span class="language_switcher_placeholder">zh_TW</span>
<span class="version_switcher_placeholder">3.7.0</span>
<a href="../index.html">Documentation </a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python 標準函式庫 (Standard Library)</a> »</li>
<li class="nav-item nav-item-2"><a href="datatypes.html" accesskey="U">8. Data Types</a> »</li>
<li class="right">
<div class="inline-search" style="display: none" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Quick search" type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('.inline-search').show(0);</script>
|
</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="module-array">
<span id="array-efficient-arrays-of-numeric-values"></span><h1>8.7. <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>這個模組定義了一個物件型別,可以簡潔的表達一個包含基本數值的陣列:字元、整數、浮點數。陣列是一個非常類似 list 的序列型態,除了陣列會限制儲存的物件型別。在建立陣列時可以使用一個字元的 <em class="dfn">type code</em> 來指定儲存的資料型別。下面是 type codes 的定義。</p>
<table border="1" class="docutils">
<colgroup>
<col width="14%" />
<col width="25%" />
<col width="24%" />
<col width="29%" />
<col width="9%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Type code</th>
<th class="head">C Type</th>
<th class="head">Python Type</th>
<th class="head">最小所需的位元組</th>
<th class="head">註解</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'b'</span></code></td>
<td>signed char</td>
<td>int</td>
<td>1</td>
<td> </td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'B'</span></code></td>
<td>unsigned char</td>
<td>int</td>
<td>1</td>
<td> </td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'u'</span></code></td>
<td>Py_UNICODE</td>
<td>Unicode character</td>
<td>2</td>
<td>(1)</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'h'</span></code></td>
<td>signed short</td>
<td>int</td>
<td>2</td>
<td> </td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'H'</span></code></td>
<td>unsigned short</td>
<td>int</td>
<td>2</td>
<td> </td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'i'</span></code></td>
<td>signed int</td>
<td>int</td>
<td>2</td>
<td> </td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'I'</span></code></td>
<td>unsigned int</td>
<td>int</td>
<td>2</td>
<td> </td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'l'</span></code></td>
<td>signed long</td>
<td>int</td>
<td>4</td>
<td> </td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'L'</span></code></td>
<td>unsigned long</td>
<td>int</td>
<td>4</td>
<td> </td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'q'</span></code></td>
<td>signed long long</td>
<td>int</td>
<td>8</td>
<td>(2)</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'Q'</span></code></td>
<td>unsigned long long</td>
<td>int</td>
<td>8</td>
<td>(2)</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">'f'</span></code></td>
<td>float</td>
<td>float</td>
<td>4</td>
<td> </td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">'d'</span></code></td>
<td>double</td>
<td>float</td>
<td>8</td>
<td> </td>
</tr>
</tbody>
</table>
<p>註解:</p>
<ol class="arabic">
<li><p class="first"><code class="docutils literal notranslate"><span class="pre">u</span></code> type 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 bits 或 32 bits。</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>
<li><p class="first">The <code class="docutils literal notranslate"><span class="pre">'q'</span></code> and <code class="docutils literal notranslate"><span class="pre">'Q'</span></code> type codes are available only if
the platform C compiler used to build Python supports C <code class="xref c c-type docutils literal notranslate"><span class="pre">long</span> <span class="pre">long</span></code>,
or, on Windows, <code class="xref c c-type docutils literal notranslate"><span class="pre">__int64</span></code>.</p>
<div class="versionadded">
<p><span class="versionmodified">3.3 版新加入.</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="descclassname">array.</code><code class="descname">array</code><span class="sig-paren">(</span><em>typecode</em><span class="optional">[</span>, <em>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> 必須是一個 list、 <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> 或包含適當型別變數的 iterable 。</p>
<p>如果指定一個 list 或 string ,新的陣列初始化時會傳入 <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>
</dd></dl>
<dl class="data">
<dt id="array.typecodes">
<code class="descclassname">array.</code><code class="descname">typecodes</code><a class="headerlink" href="#array.typecodes" title="本定義的永久連結">¶</a></dt>
<dd><p>一個包含所有可用的 type code 的字串。</p>
</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,
<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> is raised. Array objects also implement the buffer interface,
and may be used wherever <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like objects</span></a> are supported.</p>
<p>提供下方的資料物件與方法。</p>
<dl class="attribute">
<dt id="array.array.typecode">
<code class="descclassname">array.</code><code class="descname">typecode</code><a class="headerlink" href="#array.array.typecode" title="本定義的永久連結">¶</a></dt>
<dd><p>typecode 字元被用在建立陣列時。</p>
</dd></dl>
<dl class="attribute">
<dt id="array.array.itemsize">
<code class="descclassname">array.</code><code class="descname">itemsize</code><a class="headerlink" href="#array.array.itemsize" title="本定義的永久連結">¶</a></dt>
<dd><p>陣列當中的一個元素在內部需要的位元組 (bytes) 長度。</p>
</dd></dl>
<dl class="method">
<dt id="array.array.append">
<code class="descclassname">array.</code><code class="descname">append</code><span class="sig-paren">(</span><em>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="descclassname">array.</code><code class="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>回傳一個 tuple <code class="docutils literal notranslate"><span class="pre">(address,</span> <span class="pre">length)</span></code> 表示當前的記憶體位置和陣列儲存元素的緩衝區記憶體長度。緩衝區的長度單位是 bytes ,並可以用 <code class="docutils literal notranslate"><span class="pre">array.buffer_info()[1]</span> <span class="pre">*</span> <span class="pre">array.itemsize</span></code> 計算得到。這偶爾會在底層操作需要記憶體位置的輸出輸入時很有用,例如 <code class="xref c c-func docutils literal notranslate"><span class="pre">ioctl()</span></code> 指令。只要陣列存在且沒有使用任何更改長度的操作時,回傳的數值就有效。</p>
<div class="admonition note">
<p class="first admonition-title">備註</p>
<p class="last">當使用來自 C 或 C++ 程式碼(這是唯一使得這個資訊有效的途徑) 的陣列物件時,更適當的做法是使用陣列物件支援的緩衝區介面。這個方法維護了向後兼容性,並應該在新的程式碼中避免。關於緩衝區介面的文件在 <a class="reference internal" href="../c-api/buffer.html#bufferobjects"><span class="std std-ref">Buffer Protocol</span></a> 。</p>
</div>
</dd></dl>
<dl class="method">
<dt id="array.array.byteswap">
<code class="descclassname">array.</code><code class="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>「Byteswap」 所有陣列中的物件。這只有支援物件長度為 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="descclassname">array.</code><code class="descname">count</code><span class="sig-paren">(</span><em>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="descclassname">array.</code><code class="descname">extend</code><span class="sig-paren">(</span><em>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> 是另一個陣列,他必須有完全相同的 type code ,如果不同會產生 <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> 不是一個陣列,他必須可以被迭代 (iterable) 且其中的元素必須是可以被加入陣列中的正確型態。</p>
</dd></dl>
<dl class="method">
<dt id="array.array.frombytes">
<code class="descclassname">array.</code><code class="descname">frombytes</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.frombytes" title="本定義的永久連結">¶</a></dt>
<dd><p>從字串中新增元素。讀取時會將字串當作一個陣列,裡面包含了 machine value(就像從檔案中使用 <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">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="descclassname">array.</code><code class="descname">fromfile</code><span class="sig-paren">(</span><em>f</em>, <em>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> 個 machine value 類型的元素,接著將這些元素加入陣列的最尾端。如果只有少於 <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> 錯誤,但有效的元素仍然會被加入陣列中。 <em>f</em> 必須是一個真正的內建檔案物件,其他擁有 <code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code> 方法的不行。</p>
</dd></dl>
<dl class="method">
<dt id="array.array.fromlist">
<code class="descclassname">array.</code><code class="descname">fromlist</code><span class="sig-paren">(</span><em>list</em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.fromlist" title="本定義的永久連結">¶</a></dt>
<dd><p>從 list 中新增元素。這等價於 <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="descclassname">array.</code><code class="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>
</dd></dl>
<dl class="method">
<dt id="array.array.fromunicode">
<code class="descclassname">array.</code><code class="descname">fromunicode</code><span class="sig-paren">(</span><em>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="descclassname">array.</code><code class="descname">index</code><span class="sig-paren">(</span><em>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="descclassname">array.</code><code class="descname">insert</code><span class="sig-paren">(</span><em>i</em>, <em>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="descclassname">array.</code><code class="descname">pop</code><span class="sig-paren">(</span><span class="optional">[</span><em>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> 的元素。選擇性的參數 <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="descclassname">array.</code><code class="descname">remove</code><span class="sig-paren">(</span><em>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="descclassname">array.</code><code class="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="descclassname">array.</code><code class="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>將陣列轉為另一個 machine values 的陣列並回傳他的位元組表示(跟用 <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">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="descclassname">array.</code><code class="descname">tofile</code><span class="sig-paren">(</span><em>f</em><span class="sig-paren">)</span><a class="headerlink" href="#array.array.tofile" title="本定義的永久連結">¶</a></dt>
<dd><p>將所有元素 (以 machine code 的形式)寫入 <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="descclassname">array.</code><code class="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>不更改元素,將陣列轉為一般的 list 。</p>
</dd></dl>
<dl class="method">
<dt id="array.array.tostring">
<code class="descclassname">array.</code><code class="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>
</dd></dl>
<dl class="method">
<dt id="array.array.tounicode">
<code class="descclassname">array.</code><code class="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>將陣列轉為一個字串。陣列的型態必須為 <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> 將其他型態的陣列轉為字串。</p>
</dd></dl>
<p>When an array object is printed or converted to a string, it is represented as
<code class="docutils literal notranslate"><span class="pre">array(typecode,</span> <span class="pre">initializer)</span></code>. The <em>initializer</em> is omitted if the array is
empty, otherwise it is a string if the <em>typecode</em> is <code class="docutils literal notranslate"><span class="pre">'u'</span></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 <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>, so long as the
<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> class has been imported using <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>.
Examples:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">array</span><span class="p">(</span><span class="s1">'l'</span><span class="p">)</span>
<span class="n">array</span><span class="p">(</span><span class="s1">'u'</span><span class="p">,</span> <span class="s1">'hello </span><span class="se">\u2641</span><span class="s1">'</span><span class="p">)</span>
<span class="n">array</span><span class="p">(</span><span class="s1">'l'</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">'d'</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="first admonition-title">也參考</p>
<dl class="last docutils">
<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>將包含不同資料類型的二進位資料包裝與解開包裝。</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>將 External Data Representation (XDR) 的資料包裝與解開包裝,這用在一些遠端操作的系統 ( remote procedure call systems ) 。</dd>
<dt><a class="reference external" href="https://docs.scipy.org/doc/">The Numerical Python Documentation</a></dt>
<dd>Python 數值運算的擴充 (The Numeric Python extension, NumPy) 定義了另一個陣列型態,更多關於 Python 的數值運算參考 <a class="reference external" href="http://www.numpy.org/">http://www.numpy.org/</a> 。</dd>
</dl>
</div>
</div>
</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="上一章">8.6. <code class="docutils literal notranslate"><span class="pre">bisect</span></code> — 陣列二分演算法 (Array bisection algorithm)</a></p>
<h4>下個主題</h4>
<p class="topless"><a href="weakref.html"
title="下一章">8.8. <code class="docutils literal notranslate"><span class="pre">weakref</span></code> — Weak references</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">Report a Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.7/Doc/library/array.rst"
rel="nofollow">Show Source
</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="General Index"
>索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模組索引"
>模組</a> |</li>
<li class="right" >
<a href="weakref.html" title="8.8. weakref — Weak references"
>下一頁</a> |</li>
<li class="right" >
<a href="bisect.html" title="8.6. bisect — 陣列二分演算法 (Array bisection algorithm)"
>上一頁</a> |</li>
<li><img src="../_static/py.png" alt=""
style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> »</li>
<li>
<span class="language_switcher_placeholder">zh_TW</span>
<span class="version_switcher_placeholder">3.7.0</span>
<a href="../index.html">Documentation </a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python 標準函式庫 (Standard Library)</a> »</li>
<li class="nav-item nav-item-2"><a href="datatypes.html" >8. Data Types</a> »</li>
<li class="right">
<div class="inline-search" style="display: none" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Quick search" type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('.inline-search').show(0);</script>
|
</li>
</ul>
</div>
<div class="footer">
© <a href="../copyright.html">Copyright</a> 2001-2018, Python Software Foundation.
<br />
The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br />
Last updated on 8月 22, 2018.
<a href="../bugs.html">Found a bug</a>?
<br />
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.7.7.
</div>
</body>
</html>