8.6. array — 高效的数值数组

此模块定义了一种对象类型,可以紧凑地表示基本类型值的数组:字符、整数、浮点数等。 数组属于序列类型,其行为与列表非常相似,不同之处在于其中存储的对象类型是受限的。 类型在对象创建时使用单个字符的 类型码 来指定。 已定义的类型码如下:

类型码

C 类型

Python 类型

以字节表示的最小尺寸

'c'

char

character

1

'b'

signed char

int

1

'B'

unsigned char

int

1

'u'

Py_UNICODE

Unicode 字符

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

用于创建数组的类型码字符。

array.itemsize

在内部表示中一个数组项的字节长度。

array.append(x)

添加一个值为 x 的新项到数组末尾。

array.buffer_info()

返回一个元组 (address, length) 以给出用于存放数组内容的缓冲区元素的当前内存地址和长度。 以字节表示的内存缓冲区大小可通过 array.buffer_info()[1] * array.itemsize 来计算。 这在使用需要内存地址的低层级(因此不够安全) I/O 接口时会很有用,例如某些 ioctl() 操作。 只要数组存在并且没有应用改变长度的操作,返回数值就是有效的。

注解

当在 C 或 C++ 编写的代码中使用数组对象时(这是有效使用此类信息的唯一方式),使用数组对象所支持的缓冲区接口更为适宜。 此方法仅保留用作向下兼容,应避免在新代码中使用。 缓冲区接口的文档参见 Buffers and Memoryview Objects

array.byteswap()

“字节对调”所有数组项。 此方法只支持大小为 1, 2, 4 或 8 字节的值;对于其他值类型将引发 RuntimeError。 它适用于从不同字节序机器所生成的文件中读取数据的情况。

array.count(x)

返回 x 在数组中的出现次数。

array.extend(iterable)

将来自 iterable 的项添加到数组末尾。 如果 iterable 是另一个数组,它必须具有 完全 相同的类型码;否则将引发 TypeError。 如果 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)

添加来自字符串的项,将字符串解读为机器值的数组(相当于使用 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 使得 ix 在数组中首次出现的序号。

array.insert(i, x)

将值 x 作为新项插入数组的 i 位置之前。 负值将被视为相对于数组末尾的位置。

array.pop([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()

将数组转换为包含相同项的普通列表。

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) 数据。

The Numerical Python Documentation

Numeric Python 扩展 (NumPy) 定义了另一种数组类型;请访问 http://www.numpy.org/ 了解有关 Numerical Python 的更多信息。