-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.html
More file actions
482 lines (438 loc) · 36 KB
/
Copy patharray.html
File metadata and controls
482 lines (438 loc) · 36 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
476
477
478
479
480
481
482
<!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="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>8.7. array — 効率のよい数値アレイ — Python 3.6.5 ドキュメント</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">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '3.6.5',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</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.6.5 ドキュメント 内を検索"
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 — 弱参照" href="weakref.html" />
<link rel="prev" title="8.6. bisect — 配列二分法アルゴリズム" href="bisect.html" />
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
<link rel="canonical" href="https://docs.python.jp/3/library/array.html" /><script type="text/javascript">
$(document).ready(function() {
var base = 'https://docs.python.org/3.6/library/array.html';
$('a.headerlink').each(function() {
var html = '<a href="' + base + $(this).attr('href') +
'" title="原文へのリンク"><small>(原文)</small></a>';
$(this).after(html);
})
});
</script>
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/version_switch.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="総索引"
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 — 弱参照"
accesskey="N">次へ</a> |</li>
<li class="right" >
<a href="bisect.html" title="8.6. bisect — 配列二分法アルゴリズム"
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="version_switcher_placeholder">3.6.5</span>
<a href="../index.html">ドキュメント</a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python 標準ライブラリ</a> »</li>
<li class="nav-item nav-item-2"><a href="datatypes.html" accesskey="U">8. データ型</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"><span class="pre">array</span></code></a> — 効率のよい数値アレイ<a class="headerlink" href="#module-array" title="このヘッドラインへのパーマリンク">¶</a></h1>
<hr class="docutils" id="index-0" />
<p>このモジュールでは、基本的な値 (文字、整数、浮動小数点数) のアレイ (array、配列) をコンパクトに表現できるオブジェクト型を定義しています。アレイはシーケンス (sequence) 型であり、中に入れるオブジェクトの型に制限があることを除けば、リストとまったく同じように振る舞います。オブジェクト生成時に一文字の <em class="dfn">型コード</em> を用いて型を指定します。次の型コードが定義されています:</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">型コード</th>
<th class="head">C の型</th>
<th class="head">Python の型</th>
<th class="head">最小サイズ (バイト単位)</th>
<th class="head">注釈</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal"><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"><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"><span class="pre">'u'</span></code></td>
<td>Py_UNICODE</td>
<td>Unicode文字(unicode型)</td>
<td>2</td>
<td>(1)</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><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"><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"><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"><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"><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"><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"><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"><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"><span class="pre">'f'</span></code></td>
<td>浮動小数点数</td>
<td>浮動小数点数</td>
<td>4</td>
<td> </td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">'d'</span></code></td>
<td>double</td>
<td>浮動小数点数</td>
<td>8</td>
<td> </td>
</tr>
</tbody>
</table>
<p>注釈:</p>
<ol class="arabic">
<li><p class="first">タイプコード <code class="docutils literal"><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"><span class="pre">Py_UNICODE</span></code></a> あるいは <code class="xref c c-type docutils literal"><span class="pre">wchar_t</span></code>) を表します。プラットフォームに依存して、これは 16bit か 32bit になります。</p>
<p><code class="docutils literal"><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"><span class="pre">Py_UNICODE</span></code></a> API と一緒に削除されるでしょう。</p>
<div class="deprecated-removed">
<p><span class="versionmodified">バージョン 3.3 で非推奨、バージョン 4.0 で削除予定.</span></p>
</div>
</li>
<li><p class="first">タイプコード <code class="docutils literal"><span class="pre">'q'</span></code> と <code class="docutils literal"><span class="pre">'Q'</span></code> は Python をビルドしたプラットフォームのCコンパイラーが C言語の <code class="xref c c-type docutils literal"><span class="pre">long</span> <span class="pre">long</span></code> に対応しているか、 Windows で <code class="xref c c-type docutils literal"><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"><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> を渡すと初期値になりますが、 リスト、 <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"><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"><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"><span class="pre">fromunicode()</span></code></a> メソッド (以下を参照) に渡され、アレイに初期項目を追加します。それ以外の場合には、イテラブルの <em>initializer</em> は <a class="reference internal" href="#array.array.extend" title="array.array.extend"><code class="xref py py-meth docutils literal"><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>すべての利用可能なタイプコードを含む文字列</p>
</dd></dl>
<p>アレイオブジェクトでは、インデクス指定、スライス、連結および反復といった、 通常のシーケンスの演算をサポートしています。スライス代入を使うときは、 代入値は同じ型コードのアレイオブジェクトでなければなりません。 それ以外のオブジェクトを指定すると <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal"><span class="pre">TypeError</span></code></a> を送出します。 アレイオブジェクトはバッファインタフェースを実装しており、 <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like objects</span></a> をサポートしている場所ならどこでも利用できます。</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>アレイを作るときに使う型コード文字です。</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>アレイの要素 1 つの内部表現に使われるバイト長です。</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>アレイの内容を記憶するために使っているバッファの、現在のメモリアドレスと要素数の入ったタプル <code class="docutils literal"><span class="pre">(address,</span> <span class="pre">length)</span></code> を返します。バイト単位で表したメモリバッファの大きさは <code class="docutils literal"><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"><span class="pre">ioctl()</span></code> 操作のような、メモリアドレスを必要とする低レベルな (そして、本質的に危険な) I/Oインタフェースを使って作業する場合に、ときどき便利です。アレイ自体が存在し、長さを変えるような演算を適用しない限り、有効な値を返します。</p>
<div class="admonition note">
<p class="first admonition-title">注釈</p>
<p class="last">C やC++ で書いたコードからアレイオブジェクトを使う場合 (<code class="xref py py-meth docutils literal"><span class="pre">buffer_info()</span></code> の情報を使う意味のある唯一の方法です) は、アレイオブジェクトでサポートしているバッファインタフェースを使う方がより理にかなっています。このメソッドは後方互換性のために保守されており、新しいコードでの使用は避けるべきです。バッファインタフェースの説明は <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>アレイのすべての要素に対して「バイトスワップ」 (リトルエンディアンとビッグエンディアンの変換) を行います。このメソッドは大きさが 1、2、4 および 8 バイトの値のみをサポートしています。他の種類の値に使うと <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal"><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> が別のアレイ型である場合、二つのアレイは <em>全く</em> 同じ型コードでなければなりません。それ以外の場合には <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal"><span class="pre">TypeError</span></code></a> を送出します。 <em>iterable</em> がアレイでない場合、アレイに値を追加できるような正しい型の要素からなるイテレーション可能オブジェクトでなければなりません。</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>文字列から要素を追加します。文字列は、 (ファイルから <a class="reference internal" href="#array.array.fromfile" title="array.array.fromfile"><code class="xref py py-meth docutils literal"><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"><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"><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">ファイルオブジェクト</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"><span class="pre">EOFError</span></code></a> を送出しますが、それまでに読み出せた値はアレイに追加されます。 <em>f</em> は本当の組み込みファイルオブジェクトでなければなりません。 <code class="xref py py-meth docutils literal"><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>リストから要素を追加します。型に関するエラーが発生した場合にアレイが変更されないことを除き、 <code class="docutils literal"><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"><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"><span class="pre">'u'</span></code> でなければなりません。それ以外の場合には、 <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a> を送出します。他の型のアレイに Unicode 型のデータを追加するには、 <code class="docutils literal"><span class="pre">array.frombytes(unicodestring.encode(enc))</span></code> を使ってください。</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>x</em> が出現するインデクスのうち最小の値 <em>i</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> をもつ新しい要素を挿入します。 <em>i</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> の要素を取り除いて返します。オプションの引数はデフォルトで <code class="docutils literal"><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>array をマシンの値の array に変換して、 bytes の形で返します (<a class="reference internal" href="#array.array.tofile" title="array.array.tofile"><code class="xref py py-meth docutils literal"><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"><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"><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>すべての要素を (マシンの値の形式で) <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>アレイを同じ要素を持つ普通のリストに変換します。</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"><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>アレイを Unicode 文字列に変換します。アレイの型コードは <code class="docutils literal"><span class="pre">'u'</span></code> でなければなりません。それ以外の場合には <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal"><span class="pre">ValueError</span></code></a> を送出します。他の型のアレイから Unicode 文字列を得るには、 <code class="docutils literal"><span class="pre">array.tobytes().decode(enc)</span></code> を使ってください。</p>
</dd></dl>
<p>アレイオブジェクトを表示したり文字列に変換したりすると、 <code class="docutils literal"><span class="pre">array(typecode,</span> <span class="pre">initializer)</span></code> という形式で表現されます。
アレイが空の場合、 <em>initializer</em> の表示を省略します。
アレイが空でなければ、 <em>typecode</em> が <code class="docutils literal"><span class="pre">'u'</span></code> の場合には文字列に、それ以外の場合には数値のリストになります。
<a class="reference internal" href="#array.array" title="array.array"><code class="xref py py-class docutils literal"><span class="pre">array</span></code></a> クラスが <code class="docutils literal"><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"><span class="pre">eval()</span></code></a> を用いると元のアレイオブジェクトと同じデータ型と値を持つアレイに逆変換できることが保証されています。文字列表現の例を以下に示します:</p>
<div class="highlight-python3"><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"><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"><span class="pre">xdrlib</span></code></a> モジュール</dt>
<dd>遠隔手続き呼び出しシステムで使われる外部データ表現仕様 (External Data Representation, XDR) のデータのパックおよびアンパック。</dd>
<dt><a class="reference external" href="https://docs.scipy.org/doc/">Numerical Python ドキュメント</a></dt>
<dd>Numeric Python 拡張モジュール (NumPy) では、別の方法でシーケンス型を定義しています。 Numerical 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"><span class="pre">bisect</span></code> — 配列二分法アルゴリズム</a></p>
<h4>次のトピックへ</h4>
<p class="topless"><a href="weakref.html"
title="次の章へ">8.8. <code class="docutils literal"><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">バグ報告</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.6/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="8.8. weakref — 弱参照"
>次へ</a> |</li>
<li class="right" >
<a href="bisect.html" title="8.6. bisect — 配列二分法アルゴリズム"
>前へ</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="version_switcher_placeholder">3.6.5</span>
<a href="../index.html">ドキュメント</a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python 標準ライブラリ</a> »</li>
<li class="nav-item nav-item-2"><a href="datatypes.html" >8. データ型</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 />
Python Software Foundation は非営利団体です。
<a href="https://www.python.org/psf/donations/">寄付</a>
<br />
最終更新日時: 7月 29, 2018
<a href="../bugs.html">バグを見つけたら</a>?
<br />
このドキュメントは <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.6.5 を使って作成されました。
</div>
</body>
</html>