array --- 高效的数字数组


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

类型码

C 类型

Python 类型

最小字节数

备注

'b'

signed char

int

1

'B'

unsigned char

int

1

'u'

wchar_t

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'

float

float

4

'd'

double

float

8

注释:

  1. 可能为 16 位或 32 位,取决于具体的平台。

    在 3.9 版更改: array('u') now uses wchar_t as C type instead of deprecated Py_UNICODE. This change doesn't affect to its behavior because Py_UNICODE is alias of wchar_t since Python 3.3.

    Deprecated since version 3.3, will be removed in version 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 itemsize attribute.

此模块定义了以下类型:

class array.array(typecode[, initializer])

一个由 typecode 限定类型的新数组,可由可选的 initializer 初始化。initializer 必须为一个列表、bytes-like object 或在合适类型元素上迭代的可迭代对象。

如果是一个列表或字符串,该 initializer 会被传给新数组的 fromlist()frombytes()fromunicode() 方法(见下)以将初始项添加到数组中。其它将可迭代对象将被传给 extend() 方法。

引发一个 审计事件 array.__new__ 并附带参数 typecode, initializer

array.typecodes

一个由所有可用的类型码组成的字符串。

数组对象支持普通的序列操作如索引、切片、拼接和重复等。当使用切片赋值时,所赋的值必须为具有相同类型码的数组对象;所有其他情况都将引发 TypeError。数组对象也实现了缓冲区接口,可以用于所有支持 类字节对象 的场合。

The following data items and methods are also supported:

array.typecode

在创建数组时使用的类型码字符。

array.itemsize

内部表示中,单个数组项的长度。单位为字节。

array.append(x)

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

array.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++ 编写的代码中的数组对象时,才能有效利用该信息,但此时,更合理的是,使用数组对象支持的缓冲区接口。因此,该方法的存在仅仅是为了向后兼容性,应避免在新代码中使用。缓冲区接口的文档参见 缓冲协议

array.byteswap()

“字节对调”所有数组项。此方法只支持大小为 1, 2, 4 或 8 字节的值;对于其它类型的值将引发 RuntimeError。当要从另一种字节顺序的机器生成的文件中读取数据时,它很有用。

array.count(x)

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

array.extend(iterable)

将来自 iterable 的项添加到数组末尾。如果 iterable 是另一个数组,它必须具有 完全 相同的类型码;否则将引发 TypeError。如果 iterable 不是一个数组,则它必须为可迭代对象且其元素的类型须为可添加到数组的适当类型。

array.frombytes(s)

将来自字节串的项添加到数组末尾,字节串被视为由机器值组成的数组(就像用 fromfile() 方法从文件中读取数据一样)。

3.2 新版功能: fromstring() is renamed to frombytes() for clarity.

array.fromfile(f, n)

file object f 中读取 n 项(视为机器值)并将它们添加到数组末尾。如果可用的项少于 n 项,则会引发 EOFError,但可用的项仍然会被加进数组。

array.fromlist(list)

将来自列表的项添加到数组末尾。等价于 for x in list: a.append(x),而不同之处在于,若发生类型错误,数组则不会被改变。

array.fromunicode(s)

将来自 Unicode 字符串的项添加到数组末尾。数组必须是类型为 'u' 的数组;否则将引发 ValueError。请用 array.frombytes(unicodestring.encode(enc)) 来将 Unicode 数据添加到其它类型的数组。

array.index(x)

Return the smallest i such that i is the index of the first occurrence of x in the array.

array.insert(i, x)

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

array.pop([i])

从数组中移除下标为 i 的项并将其返回。参数默认值为 -1,因此默认移除并返回末项。

array.remove(x)

从数组中移除第一个出现的 x

array.reverse()

反转数组中各项的顺序。

array.tobytes()

将数组转换为一个由机器值组成的数组并返回其字节表示(和用 tofile() 方法写入文件的字节序列相同)。

3.2 新版功能: tostring() is renamed to tobytes() for clarity.

array.tofile(f)

将所有项(作为机器值)写入 file object f

array.tolist()

将数组转换为由相同的项组成的普通列表。

array.tounicode()

将数组转换为一个 Unicode 字符串。数组必须是类型为 'u' 的数组;否则将引发 ValueError。请用 array.tobytes().decode(enc) 来将其它类型的数组转换为 Unicode 字符串。

数组对象被打印或转换为字符串时,会被表示为 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 模块

对某些远程过程调用系统中使用的外部数据表示(XDR)数据进行打包和解包 。

NumPy

NumPy 包定义了另一数组类型。