8.6. array — 高效率的數值型態陣列

這個模組定義了一個物件型別,可以簡潔的表達一個包含基本數值的陣列:字元、整數、浮點數。陣列是一個非常類似 list 的序列型態,除了陣列會限制儲存的物件型別。在建立陣列時可以使用一個字元的 type code 來指定儲存的資料型別。下面是 type codes 的定義。

Type code

C Type

Python Type

最小所需的位元組

'c'

char

character

1

'b'

signed char

int

1

'B'

unsigned char

int

1

'u'

Py_UNICODE

Unicode character

2 (see note)

'h'

signed short

int

2

'H'

unsigned short

int

2

'i'

signed int

int

2

'I'

unsigned int

long

2

'l'

signed long

int

4

'L'

unsigned long

long

4

'f'

float

float

4

'd'

double

float

8

備註

The 'u' typecode corresponds to Python’s unicode character. On narrow Unicode builds this is 2-bytes, on wide builds this is 4-bytes.

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 itemsize attribute. The values stored for 'L' and 'I' items will be represented as Python long integers when retrieved, because Python’s plain integer type cannot represent the full range of C’s unsigned (long) integers.

這個模組定義了下方的型別:

class array.array(typecode[, initializer])

A new array whose items are restricted by typecode, and initialized from the optional initializer value, which must be a list, string, or iterable over elements of the appropriate type.

2.4 版更變: Formerly, only lists or strings were accepted.

If given a list or string, the initializer is passed to the new array’s fromlist(), fromstring(), or fromunicode() method (see below) to add initial items to the array. Otherwise, the iterable initializer is passed to the extend() method.

array.ArrayType

Obsolete alias for array.

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, TypeError is raised. Array objects also implement the buffer interface, and may be used wherever buffer objects are supported.

提供下方的資料物件與方法。

array.typecode

typecode 字元被用在建立陣列時。

array.itemsize

陣列當中的一個元素在內部需要的位元組 (bytes) 長度。

array.append(x)

新增一個元素 x 到陣列的最尾端。

array.buffer_info()

回傳一個 tuple (address, length) 表示當前的記憶體位置和陣列儲存元素的緩衝區記憶體長度。緩衝區的長度單位是 bytes ,並可以用 array.buffer_info()[1] * array.itemsize 計算得到。這偶爾會在底層操作需要記憶體位置的輸出輸入時很有用,例如 ioctl() 指令。只要陣列存在且沒有使用任何更改長度的操作時,回傳的數值就有效。

備註

當使用來自 C 或 C++ 程式碼(這是唯一使得這個資訊有效的途徑) 的陣列物件時,更適當的做法是使用陣列物件支援的緩衝區介面。這個方法維護了向後兼容性,並應該在新的程式碼中避免。關於緩衝區介面的文件在 Buffers and Memoryview Objects

array.byteswap()

「Byteswap」 所有陣列中的物件。這只有支援物件長度為 1、2、4 或 8 位元組的陣列,其他型別的值會導致 RuntimeError 。這在從機器讀取位元順序不同的檔案時很有用。

array.count(x)

回傳 x 在陣列中出現了幾次。

array.extend(iterable)

iterable 中新增元素到陣列的尾端,如果 iterable 是另一個陣列,他必須有完全相同的 type code ,如果不同會產生 TypeError 。如果 iterable 不是一個陣列,他必須可以被迭代 (iterable) 且其中的元素必須是可以被加入陣列中的正確型態。

2.4 版更變: Formerly, the argument could only be another array.

array.fromfile(f, n)

Read n items (as machine values) from the file object f and append them to the end of the array. If less than n items are available, EOFError is raised, but the items that were available are still inserted into the array. f must be a real built-in file object; something else with a read() method won’t do.

array.fromlist(list)

從 list 中新增元素。這等價於 for x in list: a.append(x) ,除了有型態錯誤產生時,陣列會保持原狀不會被更改。

array.fromstring(s)

從字串中新增元素。讀取時會將字串當作一個陣列,裡面包含了 machine value(就像從檔案中使用 fromfile() 方法讀出的資料)。

array.fromunicode(s)

Extends this array with data from the given unicode string. The array must be a type 'u' array; otherwise a ValueError is raised. Use array.fromstring(unicodestring.encode(enc)) to append Unicode data to an array of some other type.

array.index(x)

回傳最小的 i ,使得 i 是陣列中第一個 x 出現的索引值。

array.insert(i, x)

在位置 i 之前插入一個元素 x 。負數的索引值會從陣列尾端開始數。

array.pop([i])

移除並回傳陣列索引值 i 的元素。選擇性的參數 i 預設為 -1 ,所以預設會刪除並回傳最後一個元素。

array.read(f, n)

1.5.1 版後已棄用: Use the fromfile() method.

Read n items (as machine values) from the file object f and append them to the end of the array. If less than n items are available, EOFError is raised, but the items that were available are still inserted into the array. f must be a real built-in file object; something else with a read() method won’t do.

array.remove(x)

從陣列中刪除第一個出現的 x

array.reverse()

將整個陣列的元素按照順序逆轉。

array.tofile(f)

Write all items (as machine values) to the file object f.

array.tolist()

不更改元素,將陣列轉為一般的 list 。

array.tostring()

Convert the array to an array of machine values and return the string representation (the same sequence of bytes that would be written to a file by the tofile() method.)

array.tounicode()

Convert the array to a unicode string. The array must be a type 'u' array; otherwise a ValueError is raised. Use array.tostring().decode(enc) to obtain a unicode string from an array of some other type.

array.write(f)

1.5.1 版後已棄用: Use the tofile() method.

Write all items (as machine values) to the file object f.

When an array object is printed or converted to a string, it is represented as array(typecode, initializer). The initializer is omitted if the array is empty, otherwise it is a string if the typecode is 'c', 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 eval(), so long as the array class has been imported using from array import array. Examples:

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

也參考

模組 struct

將包含不同資料類型的二進位資料包裝與解開包裝。

模組 xdrlib

將 External Data Representation (XDR) 的資料包裝與解開包裝,這用在一些遠端操作的系統 ( remote procedure call systems ) 。

The Numerical Python Documentation

Python 數值運算的擴充 (The Numeric Python extension, NumPy) 定義了另一個陣列型態,更多關於 Python 的數值運算參考 http://www.numpy.org/