数组

array 模块可以表示整数、浮点数、字符的数组。

初始化

array(typecode,__initializer=…)
typecode表示数组的类型,文章末尾有一个参数表包含所有支持类型以及内存占用情况
initializer为初始化参数,是一个list、或者iterable、或是 bytes-like object

1
2
3
4
5
6
7
import array
"""
array_name = array.array(typecode, [initializers])
"""
arr=array.array('i',[1,2,3,4,5])
# or
arr1=array.array('i',[x for x in range(10)])

支持的操作

(官方文档)Array objects support the ordinary sequence operations of indexing(索引), slicing(切片), concatenation(拼接), and multiplication(乘法).

函数

array.typecode(数据类型)

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

array.itemsize(单个数组元素大小)

The length in bytes of(以字节为单位) one array item(数据项) in the internal(内部) representation(表示).

1
2
3
4
5
6
arr1 = array.array('i',[0,1,1,2,3])
arr2 = array.array('i',[0,1,2,3])
arr1.itemsize # 4
arr2.itemsize # 4
arr1.typecode # 'i'
#相同的不占多余的空间

array.append(x)(添加元素)

Append a new item(项) with value x to the end of the array.

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.

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. 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. This is only supported for values which are 1, 2, 4, or 8 bytes in size; for other types of values, RuntimeError is raised(引发). It 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(x发生的次数) in the array.

array.extend(iterable)(扩展另一个array)

Append items from iterable to the end of the array. If iterable is another array, it must have exactly the same type code; if not, TypeError will be raised. If iterable is not an array, it must be iterable and its elements must be the right type to be appended to the array.

1
2
3
4
5
6
7
8
arr1 = array.array('i',[i for i in range(10) if i%2==1]) #array('i', [1, 3, 5, 7, 9])
arr2 = array.array('i',[i for i in range(10) if i%2==0]) #array('i', [0, 2, 4, 6, 8])
#扩展相同type code的array对象
arr1.extend(arr2)
print(arr1) # array('i', [1, 3, 5, 7, 9, 0, 2, 4, 6, 8])
#扩展可迭代对象
arr1.extend([i for i in range(100,110) if i%2==0])
print(arr1) # array('i', [1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 100, 102, 104, 106, 108])

array.fromlist(list)

1
2
3
4
5
6
arr2 = array.array('i',[0,99])
l=[i for i in range(1000)]

arr2.fromlist(l) #与下面for循环等价
for i in l: #与上面fromlist等价
arr2.append(i)

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.

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) (将元素x插入到i前面)

Insert a new item with value x in the array before position i. 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. The optional argument defaults to -1, so that by default the last item is removed and returned.
默认是最后一个值

array.remove(x)(删除第一次出现的x)

Remove the first occurrence of x from the array.

array.reverse()(逆序)

Reverse the order of the items in the array.

array.tolist()(转换成list列表)

Convert the array to an ordinary list with the same items.

操作元素示例

1
2
3
4
5
6
7
8
9
10
import array
arr1 = array.array('i',[i for i in range(10) if i%2==1])
arr2 = array.array('i',[2,4,6,8,2])
arr1.extend(arr2) #扩展
print(arr1[0]) #第一个元素
arr1[1]=5 #改变元素
arr1.insert(1,60) #插入第二个位置
print(arr2.index(6)) #6的位置 为2
arr2.remove(2) #删除所有的2
print(arr2) # array('i', [4, 6, 8])

type code 定义

Type code C Type Python Type Minimum size in bytes Notes
‘b’ signed char int 1
‘B’ unsigned char int 1
‘u’ Py_UNICODE 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 (2)
‘Q’ unsigned long long int 8 (2)
‘f’ float float 4
‘d’ double float 8

(1)Py_UNICODE是typedef wchar_t,是16位或32位,取决于平台,已经被移除,官网不推荐使用。

(2).(官网原话)The ‘q’ and ‘Q’ type codes are available only if the platform C compiler used to build Python supports C long long, or, on Windows, __int64.
只有当用于构建Python的平台C编译器支持C long long,或者在Windows上支持int64时,’q’和’Q’类型代码才可用。__