"array" --- 効率のよい数値アレイ
********************************

======================================================================

このモジュールでは、基本的な値 (文字、整数、浮動小数点数) のアレイ
(array、配列) をコンパクトに表現できるオブジェクト型を定義しています。
アレイはシーケンス (sequence) 型であり、中に入れるオブジェクトの型に制
限があることを除けば、リストとまったく同じように振る舞います。オブジェ
クト生成時に一文字の *型コード* を用いて型を指定します。次の型コードが
定義されています:

+-------------+----------------------+---------------------+-------------------------+---------+
| 型コード    | C の型               | Python の型         | 最小サイズ (バイト単位) | 注釈    |
|=============|======================|=====================|=========================|=========|
| "'b'"       | signed char          | int                 | 1                       |         |
+-------------+----------------------+---------------------+-------------------------+---------+
| "'B'"       | unsigned char        | int                 | 1                       |         |
+-------------+----------------------+---------------------+-------------------------+---------+
| "'u'"       | wchar_t              | Unicode文字(unicode | 2                       | (1)     |
|             |                      | 型)                 |                         |         |
+-------------+----------------------+---------------------+-------------------------+---------+
| "'h'"       | signed short         | int                 | 2                       |         |
+-------------+----------------------+---------------------+-------------------------+---------+
| "'H'"       | unsigned short       | int                 | 2                       |         |
+-------------+----------------------+---------------------+-------------------------+---------+
| "'i'"       | signed int           | int                 | 2                       |         |
+-------------+----------------------+---------------------+-------------------------+---------+
| "'I'"       | unsigned int         | int                 | 2                       |         |
+-------------+----------------------+---------------------+-------------------------+---------+
| "'l'"       | signed long          | int                 | 4                       |         |
+-------------+----------------------+---------------------+-------------------------+---------+
| "'L'"       | unsigned long        | int                 | 4                       |         |
+-------------+----------------------+---------------------+-------------------------+---------+
| "'q'"       | signed long long     | int                 | 8                       |         |
+-------------+----------------------+---------------------+-------------------------+---------+
| "'Q'"       | unsigned long long   | int                 | 8                       |         |
+-------------+----------------------+---------------------+-------------------------+---------+
| "'f'"       | 浮動小数点数         | 浮動小数点数        | 4                       |         |
+-------------+----------------------+---------------------+-------------------------+---------+
| "'d'"       | double               | 浮動小数点数        | 8                       |         |
+-------------+----------------------+---------------------+-------------------------+---------+

注釈:

1. It can be 16 bits or 32 bits depending on the platform.

   バージョン 3.9 で変更: "array('u')" now uses "wchar_t" as C type
   instead of deprecated "Py_UNICODE". This change doesn't affect its
   behavior because "Py_UNICODE" is alias of "wchar_t" since Python
   3.3.

   バージョン 3.3 で非推奨、バージョン 4.0 で削除予定.

The actual representation of values is determined by the machine
architecture (strictly speaking, by the C implementation).  The actual
size can be accessed through the "array.itemsize" attribute.

The module defines the following item:

array.typecodes

   すべての利用可能なタイプコードを含む文字列

このモジュールでは次の型を定義しています:

class array.array(typecode[, initializer])

   要素のデータ型が *typecode* に限定される新しいアレイで、 オプション
   の値 *initializer* を渡すと初期値になりますが、 リスト、 *bytes-
   like object* または適当な型のイテレーション可能オブジェクトでなけれ
   ばなりません。

   リストか文字列を渡した場合、initializer は新たに作成されたアレイの
   "fromlist()" 、 "frombytes()" あるいは "fromunicode()" メソッド (以
   下を参照) に渡され、アレイに初期項目を追加します。それ以外の場合に
   は、イテラブルの *initializer* は "extend()" メソッドに渡されます。

   アレイオブジェクトでは、インデクス指定、スライス、連結および反復と
   いった、 通常のシーケンスの演算をサポートしています。スライス代入を
   使うときは、 代入値は同じ型コードのアレイオブジェクトでなければなり
   ません。 それ以外のオブジェクトを指定すると "TypeError" を送出しま
   す。 アレイオブジェクトはバッファインターフェースを実装しており、
   *bytes-like objects* をサポートしている場所ならどこでも利用できます
   。

   引数 "typecode", "initializer" 付きで 監査イベント "array.__new__"
   を送出します。

   typecode

      アレイを作るときに使う型コード文字です。

   itemsize

      アレイの要素 1 つの内部表現に使われるバイト長です。

   append(x)

      値 *x* の新たな要素をアレイの末尾に追加します。

   buffer_info()

      Return a tuple "(address, length)" giving the current memory
      address and the length in elements of the buffer used to hold
      array's contents.  The size of the memory buffer in bytes can be
      computed as "array.buffer_info()[1] * array.itemsize".  This is
      occasionally useful when working with low-level (and inherently
      unsafe) I/O interfaces that require memory addresses, such as
      certain "ioctl()" operations.  The returned numbers are valid as
      long as the array exists and no length-changing operations are
      applied to it.

      注釈:

        C やC++ で書いたコードからアレイオブジェクトを使う場合
        ("buffer_info()" の情報を使う意味のある唯一の方法です) は、ア
        レイオブジェクトでサポートしているバッファインターフェースを使
        う方がより理にかなっています。このメソッドは後方互換性のために
        保守されており、新しいコードでの使用は避けるべきです。バッファ
        インターフェースの説明は バッファプロトコル (buffer Protocol)
        にあります。

   byteswap()

      アレイのすべての要素に対して「バイトスワップ」 (リトルエンディア
      ンとビッグエンディアンの変換) を行います。このメソッドは大きさが
      1、2、4 および 8 バイトの値のみをサポートしています。他の種類の
      値に使うと "RuntimeError" を送出します。異なるバイトオーダを使う
      マシンで書かれたファイルからデータを読み込むときに役に立ちます。

   count(x)

      シーケンス中の *x* の出現回数を返します。

   extend(iterable)

      *iterable* から要素を取り出し、アレイの末尾に要素を追加します。
      *iterable* が別のアレイ型である場合、二つのアレイは *全く* 同じ
      型コードでなければなりません。それ以外の場合には "TypeError" を
      送出します。 *iterable* がアレイでない場合、アレイに値を追加でき
      るような正しい型の要素からなるイテレーション可能オブジェクトでな
      ければなりません。

   frombytes(s)

      文字列から要素を追加します。文字列は、 (ファイルから
      "fromfile()" メソッドを使って値を読み込んだときのように) マシン
      のデータ形式で表された値の配列として解釈されます。

      バージョン 3.2 で追加: "fromstring()" is renamed to
      "frombytes()" for clarity.

   fromfile(f, n)

      *ファイルオブジェクト* *f* から (マシンのデータ形式そのままで)
      *n* 個の要素を読み出し、アレイの末尾に要素を追加します。 *n* 個
      未満の要素しか読めなかった場合は "EOFError" を送出しますが、それ
      までに読み出せた値はアレイに追加されます。

   fromlist(list)

      リストから要素を追加します。型に関するエラーが発生した場合にアレ
      イが変更されないことを除き、 "for x in list: a.append(x)" と同じ
      です。

   fromunicode(s)

      指定した Unicode 文字列のデータを使ってアレイを拡張します。アレ
      イの型コードは "'u'" でなければなりません。それ以外の場合には、
      "ValueError" を送出します。他の型のアレイに Unicode 型のデータを
      追加するには、 "array.frombytes(unicodestring.encode(enc))" を使
      ってください。

   index(x[, start[, stop]])

      Return the smallest *i* such that *i* is the index of the first
      occurrence of *x* in the array.  The optional arguments *start*
      and *stop* can be specified to search for *x* within a
      subsection of the array.  Raise "ValueError" if *x* is not
      found.

      バージョン 3.10 で変更: Added optional *start* and *stop*
      parameters.

   insert(i, x)

      アレイ中の位置 *i* の前に値 *x* をもつ新しい要素を挿入します。
      *i* の値が負の場合、アレイの末尾からの相対位置として扱います。

   pop([i])

      アレイからインデクスが *i* の要素を取り除いて返します。オプショ
      ンの引数はデフォルトで "-1" になっていて、最後の要素を取り除いて
      返すようになっています。

   remove(x)

      アレイ中の *x* のうち、最初に現れたものを取り除きます。

   reverse()

      アレイの要素の順番を逆にします。

   tobytes()

      array をマシンの値の array に変換して、 bytes の形で返します
      ("tofile()" メソッドを使ってファイルに書かれるバイト列と同じです
      )。

      バージョン 3.2 で追加: "tostring()" is renamed to "tobytes()"
      for clarity.

   tofile(f)

      すべての要素を (マシンの値の形式で) *file object* *f* に書き込み
      ます。

   tolist()

      アレイを同じ要素を持つ普通のリストに変換します。

   tounicode()

      アレイを Unicode 文字列に変換します。アレイの型コードは "'u'" で
      なければなりません。それ以外の場合には "ValueError" を送出します
      。他の型のアレイから Unicode 文字列を得るには、
      "array.tobytes().decode(enc)" を使ってください。

アレイオブジェクトを表示したり文字列に変換したりすると、
"array(typecode, initializer)" という形式で表現されます。 アレイが空の
場合、 *initializer* の表示を省略します。 アレイが空でなければ、
*typecode* が "'u'" の場合には文字列に、それ以外の場合には数値のリスト
になります。 "array" クラスが "from array import array" というふうにイ
ンポートされている限り、変換後の文字列に "eval()" を用いると元のアレイ
オブジェクトと同じデータ型と値を持つアレイに逆変換できることが保証され
ています。文字列表現の例を以下に示します:

   array('l')
   array('u', 'hello \u2641')
   array('l', [1, 2, 3, 4, 5])
   array('d', [1.0, 2.0, 3.14])

参考:

  "struct" モジュール
     異なる種類のバイナリデータのパックおよびアンパック。

  "xdrlib" モジュール
     遠隔手続き呼び出しシステムで使われる外部データ表現仕様 (External
     Data Representation, XDR) のデータのパックおよびアンパック。

  NumPy
     The NumPy package defines another array type.
