arrayEfficient arrays of numeric values有效的数值数组


This module defines an object type which can compactly represent an array of basic values: characters, integers, floating point numbers. 该模块定义了一种对象类型,它可以紧凑地表示基本值的数组:字符、整数、浮点数。Arrays are sequence types and behave very much like lists, except that the type of objects stored in them is constrained. 数组是序列类型,其行为非常类似于列表,只是其中存储的对象的类型受到约束。The type is specified at object creation time by using a type code, which is a single character. 类型是在对象创建时使用类型代码指定的,类型代码是单个字符。The following type codes are defined:定义了以下类型代码:

Type code类型代码

C Type

Python Type

Minimum size in bytes最小大小(字节)

Notes笔记

'b'

signed char

int

1

'B'

unsigned char

int

1

'u'

wchar_t

Unicode character

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

Notes:备注:

  1. It can be 16 bits or 32 bits depending on the platform.它可以是16位或32位,具体取决于平台。

    Changed in version 3.9:版本3.9中更改: array('u') now uses wchar_t as C type instead of deprecated Py_UNICODE. array('u')现在使用wchar_t作为C类型,而不是不推荐的Py_UNICODEThis change doesn’t affect to its behavior because Py_UNICODE is alias of wchar_t since Python 3.3.此更改不会影响其行为,因为Py_UNICODE是自Python 3.3以来wchar_t的别名。

    Deprecated since version 3.3, will be removed in version 4.0.自版本3.3以来已弃用,将在版本4.0中删除。

The actual representation of values is determined by the machine architecture (strictly speaking, by the C implementation). 值的实际表示由机器架构决定(严格来说,由C实现决定)。The actual size can be accessed through the itemsize attribute.可以通过itemsize属性访问实际大小。

The module defines the following type:

classarray.array(typecode[, initializer])

A new array whose items are restricted by typecode, and initialized from the optional initializer value, which must be a list, a bytes-like object, or iterable over elements of the appropriate type.一个新数组,其项受typecode限制,并从可选的initializer设定项值初始化,该值必须是列表、类似字节的对象或可在适当类型的元素上读取。

If given a list or string, the initializer is passed to the new array’s fromlist(), frombytes(), or fromunicode() method (see below) to add initial items to the array. 如果给定一个列表或字符串,则将初始值设定项传递给新数组的fromlist()frombytes()fromucode()方法(见下文),以向数组中添加初始项。Otherwise, the iterable initializer is passed to the extend() method.否则,iterable初始值设定项将传递给extend()方法。

Raises an auditing event array.__new__ with arguments typecode, initializer.引发审核事件array.__new__,使用参数typecodeinitializer

array.typecodes

A string with all available type codes.包含所有可用类型代码的字符串。

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. 使用切片分配时,分配的值必须是具有相同类型代码的数组对象;在所有其他情况下,都会引发TypeErrorArray objects also implement the buffer interface, and may be used wherever bytes-like objects are supported.数组对象还实现了缓冲区接口,可以在支持类似字节的对象的任何地方使用。

The following data items and methods are also supported:还支持以下数据项和方法:

array.typecode

The typecode character used to create the array.用于创建数组的类型码字符。

array.itemsize

The length in bytes of one array item in the internal representation.内部表示中一个数组项的字节长度。

array.append(x)

Append a new item with value x to the end of the array.将值为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. 返回一个元组(address, length),给出当前内存地址和用于保存数组内容的缓冲区元素长度。The size of the memory buffer in bytes can be computed as array.buffer_info()[1] * array.itemsize. 以字节为单位的内存缓冲区大小可以计算为array.buffer_info()[1] * array.itemsizeThis is occasionally useful when working with low-level (and inherently unsafe) I/O interfaces that require memory addresses, such as certain ioctl() operations. 当处理需要内存地址的低级(本质上不安全)输入/输出接口时,例如某些ioctl()操作,这有时很有用。The returned numbers are valid as long as the array exists and no length-changing operations are applied to it.只要数组存在并且没有对其应用长度更改操作,返回的数字就有效。

Note

When using array objects from code written in C or C++ (the only way to effectively make use of this information), it makes more sense to use the buffer interface supported by array objects. 当从用C或C++编写的代码中使用数组对象时(这是有效利用这些信息的唯一方法),使用数组对象支持的缓冲区接口更有意义。This method is maintained for backward compatibility and should be avoided in new code. 这种方法是为了向后兼容而保留的,应该避免在新代码中使用。The buffer interface is documented in Buffer Protocol.缓冲接口记录在缓冲协议中。

array.byteswap()

“Byteswap” all items of the array. “Byteswap”阵列中的所有项目。This is only supported for values which are 1, 2, 4, or 8 bytes in size; for other types of values, RuntimeError is raised. 这仅支持大小为1、2、4或8字节的值;对于其他类型的值,会引发RuntimeErrorIt is useful when reading data from a file written on a machine with a different byte order.当从机器上以不同字节顺序写入的文件中读取数据时,它很有用。

array.count(x)

Return the number of occurrences of x in the array.返回数组中x的出现次数。

array.extend(iterable)

Append items from iterable to the end of the array. iterable中的项追加到数组的末尾。If iterable is another array, it must have exactly the same type code; if not, TypeError will be raised. 如果iterable是另一个数组,则它必须具有完全相同的类型代码;否则,将引发TypeErrorIf iterable is not an array, it must be iterable and its elements must be the right type to be appended to the array.如果iterable不是数组,则它必须是可迭代对象,并且其元素必须是要附加到数组的正确类型。

array.frombytes(s)

Appends items from the string, interpreting the string as an array of machine values (as if it had been read from a file using the fromfile() method).追加字符串中的项,将字符串解释为机器值数组(就像它是使用fromfile()方法从文件中读取的一样)。

New in version 3.2.版本3.2中新增。fromstring() is renamed to frombytes() for clarity.为了清晰起见,将fromstring()重命名为frombytes()

array.fromfile(f, n)

Read n items (as machine values) from the file object f and append them to the end of the array. 文件对象f中读取n个项(作为机器值),并将其附加到数组的末尾。If less than n items are available, EOFError is raised, but the items that were available are still inserted into the array.如果可用项少于n个,则会引发EOFError,但可用的项仍会插入到数组中。

array.fromlist(list)

Append items from the list. 从列表中附加项目。This is equivalent to for x in list: a.append(x) except that if there is a type error, the array is unchanged.这相当于for x in list: a.append(x),但如果存在类型错误,则数组不变。

array.fromunicode(s)

Extends this array with data from the given unicode string. 使用给定unicode字符串中的数据扩展此数组。The array must be a type 'u' array; otherwise a ValueError is raised. 数组必须是'u'型数组;否则会引发ValueErrorUse array.frombytes(unicodestring.encode(enc)) to append Unicode data to an array of some other type.使用array.frombytes(unicodestring.encode(enc))将Unicode数据附加到其他类型的数组中。

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

Return the smallest i such that i is the index of the first occurrence of x in the array. 返回最小的i,使i是数组中第一个出现的x的索引。The optional arguments start and stop can be specified to search for x within a subsection of the array. 可选参数startstop可以指定为在数组的子部分中搜索xRaise ValueError if x is not found.如果未找到x,则引发ValueError

Changed in version 3.10:版本3.10中更改: Added optional start and stop parameters.添加了可选的startstop参数。

array.insert(i, x)

Insert a new item with value x in the array before position i. 在位置i之前的数组中插入一个值为x的新项。Negative values are treated as being relative to the end of the array.负值被视为相对于数组末尾。

array.pop([i])

Removes the item with the index i from the array and returns it. 从数组中删除索引为i的项并返回它。The optional argument defaults to -1, so that by default the last item is removed and returned.可选参数默认为-1,因此默认情况下,最后一项被删除并返回。

array.remove(x)

Remove the first occurrence of x from the array.从数组中删除第一个出现的x

array.reverse()

Reverse the order of the items in the array.反转数组中项目的顺序。

array.tobytes()

Convert the array to an array of machine values and return the bytes representation (the same sequence of bytes that would be written to a file by the tofile() method.)将数组转换为机器值数组,并返回字节表示(与通过tofile()方法写入文件的字节序列相同)

New in version 3.2.版本3.2中新增。tostring() is renamed to tobytes() for clarity.为了清晰起见,将tostring()重命名为tobytes()

array.tofile(f)

Write all items (as machine values) to the file object f.将所有项目(作为机器值)写入文件对象f

array.tolist()

Convert the array to an ordinary list with the same items.将数组转换为具有相同项的普通列表。

array.tounicode()

Convert the array to a unicode string. 将数组转换为unicode字符串。The array must be a type 'u' array; otherwise a ValueError is raised. 数组必须是'u'型数组;否则会引发ValueErrorUse array.tobytes().decode(enc) to obtain a unicode string from an array of some other type.使用array.tobytes().decode(enc)从其他类型的数组中获取unicode字符串。

When an array object is printed or converted to a string, it is represented as array(typecode, initializer). 当一个数组对象被打印或转换为字符串时,它被表示为array(typecode, initializer)The initializer is omitted if the array is empty, otherwise it is a string if the typecode is 'u', otherwise it is a list of numbers. 如果数组为空,则省略initializer;否则,如果typecode'u',则省略初始值设定项;否则,它是一个数字列表。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. 只要使用from array import array导入了array类,就可以保证使用eval()将字符串转换回具有相同类型和值的数组。Examples:示例:

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

See also另请参见

Module 模块struct

Packing and unpacking of heterogeneous binary data.异构二进制数据的打包和解包。

Module 模块xdrlib

Packing and unpacking of External Data Representation (XDR) data as used in some remote procedure call systems.在某些远程过程调用系统中使用的外部数据表示(XDR)数据的打包和解包。

NumPy

The NumPy package defines another array type.NumPy包定义了另一种数组类型。